npl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright (C) 2005 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. /*!
  33. * \brief Nut Programmable Logic
  34. *
  35. * \verbatim
  36. *
  37. * $Log$
  38. * Revision 1.4 2009/01/17 11:26:46 haraldkipp
  39. * Getting rid of two remaining BSD types in favor of stdint.
  40. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  41. *
  42. * Revision 1.3 2008/08/11 06:59:42 haraldkipp
  43. * BSD types replaced by stdint types (feature request #1282721).
  44. *
  45. * Revision 1.2 2006/05/25 09:30:23 haraldkipp
  46. * Compiles for AVR. Still not tested, though.
  47. *
  48. * Revision 1.1 2006/01/05 16:30:57 haraldkipp
  49. * First check-in.
  50. *
  51. *
  52. * \endverbatim
  53. */
  54. #include <dev/npl.h>
  55. #if 0
  56. /* Use for local debugging. */
  57. #define NUTDEBUG
  58. #include <stdio.h>
  59. #endif
  60. /*!
  61. * \addtogroup xgNpl
  62. */
  63. /*@{*/
  64. static int npl_registered;
  65. /*
  66. * NIC interrupt entry.
  67. */
  68. static void NplInterrupt(void *arg)
  69. {
  70. uint16_t slr;
  71. /* Read signal latch register and clear all enabled interrupts. */
  72. slr = inw(NPL_SLR) & inw(NPL_IMR);
  73. /* RS232 CTS activated. */
  74. if (slr & NPL_RSCTS) {
  75. if (sig_RSCTS.ir_handler) {
  76. (sig_RSCTS.ir_handler) (sig_RSCTS.ir_arg);
  77. }
  78. }
  79. /* RS232 DSR activated. */
  80. if (slr & NPL_RSDSR) {
  81. if (sig_RSDSR.ir_handler) {
  82. (sig_RSDSR.ir_handler) (sig_RSDSR.ir_arg);
  83. }
  84. }
  85. /* RS232 DCD activated. */
  86. if (slr & NPL_RSDCD) {
  87. if (sig_RSDCD.ir_handler) {
  88. (sig_RSDCD.ir_handler) (sig_RSDCD.ir_arg);
  89. }
  90. }
  91. /* RS232 RI activated. */
  92. if (slr & NPL_RSRI) {
  93. if (sig_RSRI.ir_handler) {
  94. (sig_RSRI.ir_handler) (sig_RSRI.ir_arg);
  95. }
  96. }
  97. /* RTC Alarm activated. */
  98. if (slr & NPL_RTCALARM) {
  99. if (sig_RTCALARM.ir_handler) {
  100. (sig_RTCALARM.ir_handler) (sig_RTCALARM.ir_arg);
  101. }
  102. }
  103. /* LAN wakeup activated. */
  104. if (slr & NPL_LANWAKEUP) {
  105. if (sig_LANWAKEUP.ir_handler) {
  106. (sig_LANWAKEUP.ir_handler) (sig_LANWAKEUP.ir_arg);
  107. }
  108. }
  109. /* FLASH ready activated. */
  110. if (slr & NPL_FMBUSY) {
  111. if (sig_FMBUSY.ir_handler) {
  112. (sig_FMBUSY.ir_handler) (sig_FMBUSY.ir_arg);
  113. }
  114. }
  115. /* RS232 signal valid. */
  116. if (slr & NPL_RSINVAL) {
  117. if (sig_RSINVAL.ir_handler) {
  118. (sig_RSINVAL.ir_handler) (sig_RSINVAL.ir_arg);
  119. }
  120. }
  121. /* RS232 signal invalid. */
  122. if (slr & NPL_NRSINVAL) {
  123. if (sig_NRSINVAL.ir_handler) {
  124. (sig_NRSINVAL.ir_handler) (sig_NRSINVAL.ir_arg);
  125. }
  126. }
  127. /* Multimedia card inserted. */
  128. if (slr & NPL_MMCD) {
  129. if (sig_MMCD.ir_handler) {
  130. (sig_MMCD.ir_handler) (sig_MMCD.ir_arg);
  131. }
  132. }
  133. /* Multimedia card removed. */
  134. if (slr & NPL_NMMCD) {
  135. if (sig_NMMCD.ir_handler) {
  136. (sig_NMMCD.ir_handler) (sig_NMMCD.ir_arg);
  137. }
  138. }
  139. outw(NPL_SCR, slr);
  140. }
  141. static int NplIrqCtl(int cmd, void *param, IRQ_HANDLER * irq, unsigned int mask)
  142. {
  143. int rc = 0;
  144. unsigned int *ival = (unsigned int *) param;
  145. uint16_t enabled = inw(NPL_IMR) & mask;
  146. /* Disable interrupt. */
  147. if (enabled) {
  148. outw(NPL_IMR, enabled & ~mask);
  149. }
  150. switch (cmd) {
  151. case NUT_IRQCTL_INIT:
  152. /* Clear interrupt */
  153. outw(NPL_SCR, mask);
  154. break;
  155. case NUT_IRQCTL_STATUS:
  156. if (enabled) {
  157. *ival |= 1;
  158. } else {
  159. *ival &= ~1;
  160. }
  161. break;
  162. case NUT_IRQCTL_ENABLE:
  163. enabled = 1;
  164. break;
  165. case NUT_IRQCTL_DISABLE:
  166. enabled = 0;
  167. break;
  168. #ifdef NUT_PERFMON
  169. case NUT_IRQCTL_GETCOUNT:
  170. *ival = (unsigned int) irq->ir_count;
  171. irq->ir_count = 0;
  172. break;
  173. #endif
  174. default:
  175. rc = -1;
  176. break;
  177. }
  178. /* Enable interrupt. */
  179. if (enabled) {
  180. outw(NPL_IMR, inw(NPL_IMR) | mask);
  181. }
  182. return rc;
  183. }
  184. static int NplRsCtsCtl(int cmd, void *param)
  185. {
  186. return NplIrqCtl(cmd, param, &sig_RSCTS, NPL_RSCTS);
  187. }
  188. static int NplRsDsrCtl(int cmd, void *param)
  189. {
  190. return NplIrqCtl(cmd, param, &sig_RSDSR, NPL_RSDSR);
  191. }
  192. static int NplRsDcdCtl(int cmd, void *param)
  193. {
  194. return NplIrqCtl(cmd, param, &sig_RSDCD, NPL_RSDCD);
  195. }
  196. static int NplRsRiCtl(int cmd, void *param)
  197. {
  198. return NplIrqCtl(cmd, param, &sig_RSRI, NPL_RSRI);
  199. }
  200. static int NplRtcAlarmCtl(int cmd, void *param)
  201. {
  202. return NplIrqCtl(cmd, param, &sig_RTCALARM, NPL_RTCALARM);
  203. }
  204. static int NplLanWakeupCtl(int cmd, void *param)
  205. {
  206. return NplIrqCtl(cmd, param, &sig_LANWAKEUP, NPL_LANWAKEUP);
  207. }
  208. static int NplFmBusyCtl(int cmd, void *param)
  209. {
  210. return NplIrqCtl(cmd, param, &sig_FMBUSY, NPL_FMBUSY);
  211. }
  212. static int NplRsInvalCtl(int cmd, void *param)
  213. {
  214. return NplIrqCtl(cmd, param, &sig_RSINVAL, NPL_RSINVAL);
  215. }
  216. static int NplRsValidCtl(int cmd, void *param)
  217. {
  218. return NplIrqCtl(cmd, param, &sig_NRSINVAL, NPL_NRSINVAL);
  219. }
  220. static int NplMmcInsertCtl(int cmd, void *param)
  221. {
  222. return NplIrqCtl(cmd, param, &sig_MMCD, NPL_MMCD);
  223. }
  224. static int NplMmcRemoveCtl(int cmd, void *param)
  225. {
  226. return NplIrqCtl(cmd, param, &sig_NMMCD, NPL_NMMCD);
  227. }
  228. /*!
  229. * \brief Register an NPL interrupt handler.
  230. *
  231. * This function is typically called by device drivers, but
  232. * applications may also implement their local interrupt
  233. * handlers.
  234. *
  235. * \param irq Interrupt to be associated with this handler.
  236. * \param handler This routine will be called by Nut/OS, when the
  237. * specified interrupt occurs.
  238. * \param arg Argument to be passed to the interrupt handler.
  239. *
  240. * \return 0 on success, -1 otherwise.
  241. */
  242. int NplRegisterIrqHandler(IRQ_HANDLER * irq, void (*handler) (void *), void *arg)
  243. {
  244. int rc;
  245. /* Initialize this interrupt. */
  246. rc = (irq->ir_ctl) (NUT_IRQCTL_INIT, NULL);
  247. if (rc == 0) {
  248. /* Set the interrupt handler. */
  249. irq->ir_arg = arg;
  250. irq->ir_handler = handler;
  251. if (!npl_registered) {
  252. npl_registered = 1;
  253. #if defined(ETHERNUT3)
  254. outr(PIO_PDR, _BV(9));
  255. #endif
  256. if ((rc = NutRegisterIrqHandler(&sig_INTERRUPT0, NplInterrupt, NULL)) == 0) {
  257. NutIrqSetMode(&sig_INTERRUPT0, NUT_IRQMODE_LOWLEVEL);
  258. NutIrqEnable(&sig_INTERRUPT0);
  259. }
  260. }
  261. }
  262. return rc;
  263. }
  264. /*!
  265. * \brief Enable a specified NPL interrupt.
  266. *
  267. * \param irq Interrupt to enable.
  268. *
  269. * \return 0 on success, -1 otherwise.
  270. */
  271. int NplIrqEnable(IRQ_HANDLER * irq)
  272. {
  273. return (irq->ir_ctl) (NUT_IRQCTL_ENABLE, NULL);
  274. }
  275. /*!
  276. * \brief Disable a specified NPL interrupt.
  277. *
  278. * \param irq Interrupt to disable.
  279. *
  280. * \return 0 on success, -1 otherwise.
  281. */
  282. int NplIrqDisable(IRQ_HANDLER * irq)
  283. {
  284. return (irq->ir_ctl) (NUT_IRQCTL_DISABLE, NULL);
  285. }
  286. /*!
  287. * \brief RS232 CTS interrupt handler info.
  288. */
  289. IRQ_HANDLER sig_RSCTS = {
  290. #ifdef NUT_PERFMON
  291. 0, /* Interrupt counter, ir_count. */
  292. #endif
  293. NULL, /* Passed argument, ir_arg. */
  294. NULL, /* Handler subroutine, ir_handler. */
  295. NplRsCtsCtl /* Interrupt control, ir_ctl. */
  296. };
  297. /*!
  298. * \brief RS232 DSR interrupt handler info.
  299. */
  300. IRQ_HANDLER sig_RSDSR = {
  301. #ifdef NUT_PERFMON
  302. 0, /* Interrupt counter, ir_count. */
  303. #endif
  304. NULL, /* Passed argument, ir_arg. */
  305. NULL, /* Handler subroutine, ir_handler. */
  306. NplRsDsrCtl /* Interrupt control, ir_ctl. */
  307. };
  308. /*!
  309. * \brief RS232 DCD interrupt handler info.
  310. */
  311. IRQ_HANDLER sig_RSDCD = {
  312. #ifdef NUT_PERFMON
  313. 0, /* Interrupt counter, ir_count. */
  314. #endif
  315. NULL, /* Passed argument, ir_arg. */
  316. NULL, /* Handler subroutine, ir_handler. */
  317. NplRsDcdCtl /* Interrupt control, ir_ctl. */
  318. };
  319. /*!
  320. * \brief RS232 RI interrupt handler info.
  321. */
  322. IRQ_HANDLER sig_RSRI = {
  323. #ifdef NUT_PERFMON
  324. 0, /* Interrupt counter, ir_count. */
  325. #endif
  326. NULL, /* Passed argument, ir_arg. */
  327. NULL, /* Handler subroutine, ir_handler. */
  328. NplRsRiCtl /* Interrupt control, ir_ctl. */
  329. };
  330. /*!
  331. * \brief RTC alarm interrupt handler info.
  332. */
  333. IRQ_HANDLER sig_RTCALARM = {
  334. #ifdef NUT_PERFMON
  335. 0, /* Interrupt counter, ir_count. */
  336. #endif
  337. NULL, /* Passed argument, ir_arg. */
  338. NULL, /* Handler subroutine, ir_handler. */
  339. NplRtcAlarmCtl /* Interrupt control, ir_ctl. */
  340. };
  341. /*!
  342. * \brief LAN wakeup interrupt handler info.
  343. */
  344. IRQ_HANDLER sig_LANWAKEUP = {
  345. #ifdef NUT_PERFMON
  346. 0, /* Interrupt counter, ir_count. */
  347. #endif
  348. NULL, /* Passed argument, ir_arg. */
  349. NULL, /* Handler subroutine, ir_handler. */
  350. NplLanWakeupCtl /* Interrupt control, ir_ctl. */
  351. };
  352. /*!
  353. * \brief Flash memory busy interrupt handler info.
  354. */
  355. IRQ_HANDLER sig_FMBUSY = {
  356. #ifdef NUT_PERFMON
  357. 0, /* Interrupt counter, ir_count. */
  358. #endif
  359. NULL, /* Passed argument, ir_arg. */
  360. NULL, /* Handler subroutine, ir_handler. */
  361. NplFmBusyCtl /* Interrupt control, ir_ctl. */
  362. };
  363. /*!
  364. * \brief RS232 signal invalid interrupt handler info.
  365. */
  366. IRQ_HANDLER sig_RSINVAL = {
  367. #ifdef NUT_PERFMON
  368. 0, /* Interrupt counter, ir_count. */
  369. #endif
  370. NULL, /* Passed argument, ir_arg. */
  371. NULL, /* Handler subroutine, ir_handler. */
  372. NplRsInvalCtl /* Interrupt control, ir_ctl. */
  373. };
  374. /*!
  375. * \brief RS232 signal valid interrupt handler info.
  376. */
  377. IRQ_HANDLER sig_NRSINVAL = {
  378. #ifdef NUT_PERFMON
  379. 0, /* Interrupt counter, ir_count. */
  380. #endif
  381. NULL, /* Passed argument, ir_arg. */
  382. NULL, /* Handler subroutine, ir_handler. */
  383. NplRsValidCtl /* Interrupt control, ir_ctl. */
  384. };
  385. /*!
  386. * \brief Multimedia card insertion interrupt handler info.
  387. */
  388. IRQ_HANDLER sig_MMCD = {
  389. #ifdef NUT_PERFMON
  390. 0, /* Interrupt counter, ir_count. */
  391. #endif
  392. NULL, /* Passed argument, ir_arg. */
  393. NULL, /* Handler subroutine, ir_handler. */
  394. NplMmcInsertCtl /* Interrupt control, ir_ctl. */
  395. };
  396. /*!
  397. * \brief Multimedia card removal interrupt handler info.
  398. */
  399. IRQ_HANDLER sig_NMMCD = {
  400. #ifdef NUT_PERFMON
  401. 0, /* Interrupt counter, ir_count. */
  402. #endif
  403. NULL, /* Passed argument, ir_arg. */
  404. NULL, /* Handler subroutine, ir_handler. */
  405. NplMmcRemoveCtl /* Interrupt control, ir_ctl. */
  406. };
  407. /*@}*/