hp_sdc_mlc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Access to HP-HIL MLC through HP System Device Controller.
  3. *
  4. * Copyright (c) 2001 Brian S. Julin
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * Alternatively, this software may be distributed under the terms of the
  17. * GNU General Public License ("GPL").
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. *
  29. * References:
  30. * HP-HIL Technical Reference Manual. Hewlett Packard Product No. 45918A
  31. * System Device Controller Microprocessor Firmware Theory of Operation
  32. * for Part Number 1820-4784 Revision B. Dwg No. A-1820-4784-2
  33. *
  34. */
  35. #include <linux/hil_mlc.h>
  36. #include <linux/hp_sdc.h>
  37. #include <linux/errno.h>
  38. #include <linux/kernel.h>
  39. #include <linux/module.h>
  40. #include <linux/init.h>
  41. #include <linux/string.h>
  42. #include <linux/semaphore.h>
  43. #define PREFIX "HP SDC MLC: "
  44. static hil_mlc hp_sdc_mlc;
  45. MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
  46. MODULE_DESCRIPTION("Glue for onboard HIL MLC in HP-PARISC machines");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. static struct hp_sdc_mlc_priv_s {
  49. int emtestmode;
  50. hp_sdc_transaction trans;
  51. u8 tseq[16];
  52. int got5x;
  53. } hp_sdc_mlc_priv;
  54. /************************* Interrupt context ******************************/
  55. static void hp_sdc_mlc_isr (int irq, void *dev_id,
  56. uint8_t status, uint8_t data)
  57. {
  58. int idx;
  59. hil_mlc *mlc = &hp_sdc_mlc;
  60. write_lock(&mlc->lock);
  61. if (mlc->icount < 0) {
  62. printk(KERN_WARNING PREFIX "HIL Overflow!\n");
  63. up(&mlc->isem);
  64. goto out;
  65. }
  66. idx = 15 - mlc->icount;
  67. if ((status & HP_SDC_STATUS_IRQMASK) == HP_SDC_STATUS_HILDATA) {
  68. mlc->ipacket[idx] |= data | HIL_ERR_INT;
  69. mlc->icount--;
  70. if (hp_sdc_mlc_priv.got5x || !idx)
  71. goto check;
  72. if ((mlc->ipacket[idx - 1] & HIL_PKT_ADDR_MASK) !=
  73. (mlc->ipacket[idx] & HIL_PKT_ADDR_MASK)) {
  74. mlc->ipacket[idx] &= ~HIL_PKT_ADDR_MASK;
  75. mlc->ipacket[idx] |= (mlc->ipacket[idx - 1]
  76. & HIL_PKT_ADDR_MASK);
  77. }
  78. goto check;
  79. }
  80. /* We know status is 5X */
  81. if (data & HP_SDC_HIL_ISERR)
  82. goto err;
  83. mlc->ipacket[idx] =
  84. (data & HP_SDC_HIL_R1MASK) << HIL_PKT_ADDR_SHIFT;
  85. hp_sdc_mlc_priv.got5x = 1;
  86. goto out;
  87. check:
  88. hp_sdc_mlc_priv.got5x = 0;
  89. if (mlc->imatch == 0)
  90. goto done;
  91. if ((mlc->imatch == (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_POL))
  92. && (mlc->ipacket[idx] == (mlc->imatch | idx)))
  93. goto done;
  94. if (mlc->ipacket[idx] == mlc->imatch)
  95. goto done;
  96. goto out;
  97. err:
  98. printk(KERN_DEBUG PREFIX "err code %x\n", data);
  99. switch (data) {
  100. case HP_SDC_HIL_RC_DONE:
  101. printk(KERN_WARNING PREFIX "Bastard SDC reconfigured loop!\n");
  102. break;
  103. case HP_SDC_HIL_ERR:
  104. mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_PERR |
  105. HIL_ERR_FERR | HIL_ERR_FOF;
  106. break;
  107. case HP_SDC_HIL_TO:
  108. mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_LERR;
  109. break;
  110. case HP_SDC_HIL_RC:
  111. printk(KERN_WARNING PREFIX "Bastard SDC decided to reconfigure loop!\n");
  112. break;
  113. default:
  114. printk(KERN_WARNING PREFIX "Unknown HIL Error status (%x)!\n", data);
  115. break;
  116. }
  117. /* No more data will be coming due to an error. */
  118. done:
  119. tasklet_schedule(mlc->tasklet);
  120. up(&mlc->isem);
  121. out:
  122. write_unlock(&mlc->lock);
  123. }
  124. /******************** Tasklet or userspace context functions ****************/
  125. static int hp_sdc_mlc_in(hil_mlc *mlc, suseconds_t timeout)
  126. {
  127. struct hp_sdc_mlc_priv_s *priv;
  128. int rc = 2;
  129. priv = mlc->priv;
  130. /* Try to down the semaphore */
  131. if (down_trylock(&mlc->isem)) {
  132. struct timeval tv;
  133. if (priv->emtestmode) {
  134. mlc->ipacket[0] =
  135. HIL_ERR_INT | (mlc->opacket &
  136. (HIL_PKT_CMD |
  137. HIL_PKT_ADDR_MASK |
  138. HIL_PKT_DATA_MASK));
  139. mlc->icount = 14;
  140. /* printk(KERN_DEBUG PREFIX ">[%x]\n", mlc->ipacket[0]); */
  141. goto wasup;
  142. }
  143. do_gettimeofday(&tv);
  144. tv.tv_usec += USEC_PER_SEC * (tv.tv_sec - mlc->instart.tv_sec);
  145. if (tv.tv_usec - mlc->instart.tv_usec > mlc->intimeout) {
  146. /* printk("!%i %i",
  147. tv.tv_usec - mlc->instart.tv_usec,
  148. mlc->intimeout);
  149. */
  150. rc = 1;
  151. up(&mlc->isem);
  152. }
  153. goto done;
  154. }
  155. wasup:
  156. up(&mlc->isem);
  157. rc = 0;
  158. done:
  159. return rc;
  160. }
  161. static int hp_sdc_mlc_cts(hil_mlc *mlc)
  162. {
  163. struct hp_sdc_mlc_priv_s *priv;
  164. priv = mlc->priv;
  165. /* Try to down the semaphores -- they should be up. */
  166. BUG_ON(down_trylock(&mlc->isem));
  167. BUG_ON(down_trylock(&mlc->osem));
  168. up(&mlc->isem);
  169. up(&mlc->osem);
  170. if (down_trylock(&mlc->csem)) {
  171. if (priv->trans.act.semaphore != &mlc->csem)
  172. goto poll;
  173. else
  174. goto busy;
  175. }
  176. if (!(priv->tseq[4] & HP_SDC_USE_LOOP))
  177. goto done;
  178. poll:
  179. priv->trans.act.semaphore = &mlc->csem;
  180. priv->trans.actidx = 0;
  181. priv->trans.idx = 1;
  182. priv->trans.endidx = 5;
  183. priv->tseq[0] =
  184. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
  185. priv->tseq[1] = HP_SDC_CMD_READ_USE;
  186. priv->tseq[2] = 1;
  187. priv->tseq[3] = 0;
  188. priv->tseq[4] = 0;
  189. __hp_sdc_enqueue_transaction(&priv->trans);
  190. busy:
  191. return 1;
  192. done:
  193. priv->trans.act.semaphore = &mlc->osem;
  194. up(&mlc->csem);
  195. return 0;
  196. }
  197. static void hp_sdc_mlc_out(hil_mlc *mlc)
  198. {
  199. struct hp_sdc_mlc_priv_s *priv;
  200. priv = mlc->priv;
  201. /* Try to down the semaphore -- it should be up. */
  202. BUG_ON(down_trylock(&mlc->osem));
  203. if (mlc->opacket & HIL_DO_ALTER_CTRL)
  204. goto do_control;
  205. do_data:
  206. if (priv->emtestmode) {
  207. up(&mlc->osem);
  208. return;
  209. }
  210. /* Shouldn't be sending commands when loop may be busy */
  211. BUG_ON(down_trylock(&mlc->csem));
  212. up(&mlc->csem);
  213. priv->trans.actidx = 0;
  214. priv->trans.idx = 1;
  215. priv->trans.act.semaphore = &mlc->osem;
  216. priv->trans.endidx = 6;
  217. priv->tseq[0] =
  218. HP_SDC_ACT_DATAREG | HP_SDC_ACT_POSTCMD | HP_SDC_ACT_SEMAPHORE;
  219. priv->tseq[1] = 0x7;
  220. priv->tseq[2] =
  221. (mlc->opacket &
  222. (HIL_PKT_ADDR_MASK | HIL_PKT_CMD))
  223. >> HIL_PKT_ADDR_SHIFT;
  224. priv->tseq[3] =
  225. (mlc->opacket & HIL_PKT_DATA_MASK)
  226. >> HIL_PKT_DATA_SHIFT;
  227. priv->tseq[4] = 0; /* No timeout */
  228. if (priv->tseq[3] == HIL_CMD_DHR)
  229. priv->tseq[4] = 1;
  230. priv->tseq[5] = HP_SDC_CMD_DO_HIL;
  231. goto enqueue;
  232. do_control:
  233. priv->emtestmode = mlc->opacket & HIL_CTRL_TEST;
  234. /* we cannot emulate this, it should not be used. */
  235. BUG_ON((mlc->opacket & (HIL_CTRL_APE | HIL_CTRL_IPF)) == HIL_CTRL_APE);
  236. if ((mlc->opacket & HIL_CTRL_ONLY) == HIL_CTRL_ONLY)
  237. goto control_only;
  238. /* Should not send command/data after engaging APE */
  239. BUG_ON(mlc->opacket & HIL_CTRL_APE);
  240. /* Disengaging APE this way would not be valid either since
  241. * the loop must be allowed to idle.
  242. *
  243. * So, it works out that we really never actually send control
  244. * and data when using SDC, we just send the data.
  245. */
  246. goto do_data;
  247. control_only:
  248. priv->trans.actidx = 0;
  249. priv->trans.idx = 1;
  250. priv->trans.act.semaphore = &mlc->osem;
  251. priv->trans.endidx = 4;
  252. priv->tseq[0] =
  253. HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
  254. priv->tseq[1] = HP_SDC_CMD_SET_LPC;
  255. priv->tseq[2] = 1;
  256. /* priv->tseq[3] = (mlc->ddc + 1) | HP_SDC_LPS_ACSUCC; */
  257. priv->tseq[3] = 0;
  258. if (mlc->opacket & HIL_CTRL_APE) {
  259. priv->tseq[3] |= HP_SDC_LPC_APE_IPF;
  260. BUG_ON(down_trylock(&mlc->csem));
  261. }
  262. enqueue:
  263. hp_sdc_enqueue_transaction(&priv->trans);
  264. }
  265. static int __init hp_sdc_mlc_init(void)
  266. {
  267. hil_mlc *mlc = &hp_sdc_mlc;
  268. int err;
  269. #ifdef __mc68000__
  270. if (!MACH_IS_HP300)
  271. return -ENODEV;
  272. #endif
  273. printk(KERN_INFO PREFIX "Registering the System Domain Controller's HIL MLC.\n");
  274. hp_sdc_mlc_priv.emtestmode = 0;
  275. hp_sdc_mlc_priv.trans.seq = hp_sdc_mlc_priv.tseq;
  276. hp_sdc_mlc_priv.trans.act.semaphore = &mlc->osem;
  277. hp_sdc_mlc_priv.got5x = 0;
  278. mlc->cts = &hp_sdc_mlc_cts;
  279. mlc->in = &hp_sdc_mlc_in;
  280. mlc->out = &hp_sdc_mlc_out;
  281. mlc->priv = &hp_sdc_mlc_priv;
  282. err = hil_mlc_register(mlc);
  283. if (err) {
  284. printk(KERN_WARNING PREFIX "Failed to register MLC structure with hil_mlc\n");
  285. return err;
  286. }
  287. if (hp_sdc_request_hil_irq(&hp_sdc_mlc_isr)) {
  288. printk(KERN_WARNING PREFIX "Request for raw HIL ISR hook denied\n");
  289. if (hil_mlc_unregister(mlc))
  290. printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
  291. "This is bad. Could cause an oops.\n");
  292. return -EBUSY;
  293. }
  294. return 0;
  295. }
  296. static void __exit hp_sdc_mlc_exit(void)
  297. {
  298. hil_mlc *mlc = &hp_sdc_mlc;
  299. if (hp_sdc_release_hil_irq(&hp_sdc_mlc_isr))
  300. printk(KERN_ERR PREFIX "Failed to release the raw HIL ISR hook.\n"
  301. "This is bad. Could cause an oops.\n");
  302. if (hil_mlc_unregister(mlc))
  303. printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
  304. "This is bad. Could cause an oops.\n");
  305. }
  306. module_init(hp_sdc_mlc_init);
  307. module_exit(hp_sdc_mlc_exit);