hp_sdc_mlc.c 9.7 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. #define PREFIX "HP SDC MLC: "
  43. static hil_mlc hp_sdc_mlc;
  44. MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
  45. MODULE_DESCRIPTION("Glue for onboard HIL MLC in HP-PARISC machines");
  46. MODULE_LICENSE("Dual BSD/GPL");
  47. struct hp_sdc_mlc_priv_s {
  48. int emtestmode;
  49. hp_sdc_transaction trans;
  50. u8 tseq[16];
  51. int got5x;
  52. } hp_sdc_mlc_priv;
  53. /************************* Interrupt context ******************************/
  54. static void hp_sdc_mlc_isr (int irq, void *dev_id,
  55. uint8_t status, uint8_t data) {
  56. int idx;
  57. hil_mlc *mlc = &hp_sdc_mlc;
  58. write_lock(&(mlc->lock));
  59. if (mlc->icount < 0) {
  60. printk(KERN_WARNING PREFIX "HIL Overflow!\n");
  61. up(&mlc->isem);
  62. goto out;
  63. }
  64. idx = 15 - mlc->icount;
  65. if ((status & HP_SDC_STATUS_IRQMASK) == HP_SDC_STATUS_HILDATA) {
  66. mlc->ipacket[idx] |= data | HIL_ERR_INT;
  67. mlc->icount--;
  68. if (hp_sdc_mlc_priv.got5x) goto check;
  69. if (!idx) goto check;
  70. if ((mlc->ipacket[idx-1] & HIL_PKT_ADDR_MASK) !=
  71. (mlc->ipacket[idx] & HIL_PKT_ADDR_MASK)) {
  72. mlc->ipacket[idx] &= ~HIL_PKT_ADDR_MASK;
  73. mlc->ipacket[idx] |= (mlc->ipacket[idx-1]
  74. & HIL_PKT_ADDR_MASK);
  75. }
  76. goto check;
  77. }
  78. /* We know status is 5X */
  79. if (data & HP_SDC_HIL_ISERR) goto err;
  80. mlc->ipacket[idx] =
  81. (data & HP_SDC_HIL_R1MASK) << HIL_PKT_ADDR_SHIFT;
  82. hp_sdc_mlc_priv.got5x = 1;
  83. goto out;
  84. check:
  85. hp_sdc_mlc_priv.got5x = 0;
  86. if (mlc->imatch == 0) goto done;
  87. if ((mlc->imatch == (HIL_ERR_INT | HIL_PKT_CMD | HIL_CMD_POL))
  88. && (mlc->ipacket[idx] == (mlc->imatch | idx))) goto done;
  89. if (mlc->ipacket[idx] == mlc->imatch) goto done;
  90. goto out;
  91. err:
  92. printk(KERN_DEBUG PREFIX "err code %x\n", data);
  93. switch (data) {
  94. case HP_SDC_HIL_RC_DONE:
  95. printk(KERN_WARNING PREFIX "Bastard SDC reconfigured loop!\n");
  96. break;
  97. case HP_SDC_HIL_ERR:
  98. mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_PERR |
  99. HIL_ERR_FERR | HIL_ERR_FOF;
  100. break;
  101. case HP_SDC_HIL_TO:
  102. mlc->ipacket[idx] |= HIL_ERR_INT | HIL_ERR_LERR;
  103. break;
  104. case HP_SDC_HIL_RC:
  105. printk(KERN_WARNING PREFIX "Bastard SDC decided to reconfigure loop!\n");
  106. break;
  107. default:
  108. printk(KERN_WARNING PREFIX "Unkown HIL Error status (%x)!\n", data);
  109. break;
  110. }
  111. /* No more data will be coming due to an error. */
  112. done:
  113. tasklet_schedule(mlc->tasklet);
  114. up(&(mlc->isem));
  115. out:
  116. write_unlock(&(mlc->lock));
  117. }
  118. /******************** Tasklet or userspace context functions ****************/
  119. static int hp_sdc_mlc_in (hil_mlc *mlc, suseconds_t timeout) {
  120. unsigned long flags;
  121. struct hp_sdc_mlc_priv_s *priv;
  122. int rc = 2;
  123. priv = mlc->priv;
  124. write_lock_irqsave(&(mlc->lock), flags);
  125. /* Try to down the semaphore */
  126. if (down_trylock(&(mlc->isem))) {
  127. struct timeval tv;
  128. if (priv->emtestmode) {
  129. mlc->ipacket[0] =
  130. HIL_ERR_INT | (mlc->opacket &
  131. (HIL_PKT_CMD |
  132. HIL_PKT_ADDR_MASK |
  133. HIL_PKT_DATA_MASK));
  134. mlc->icount = 14;
  135. /* printk(KERN_DEBUG PREFIX ">[%x]\n", mlc->ipacket[0]); */
  136. goto wasup;
  137. }
  138. do_gettimeofday(&tv);
  139. tv.tv_usec += 1000000 * (tv.tv_sec - mlc->instart.tv_sec);
  140. if (tv.tv_usec - mlc->instart.tv_usec > mlc->intimeout) {
  141. /* printk("!%i %i",
  142. tv.tv_usec - mlc->instart.tv_usec,
  143. mlc->intimeout);
  144. */
  145. rc = 1;
  146. up(&(mlc->isem));
  147. }
  148. goto done;
  149. }
  150. wasup:
  151. up(&(mlc->isem));
  152. rc = 0;
  153. goto done;
  154. done:
  155. write_unlock_irqrestore(&(mlc->lock), flags);
  156. return rc;
  157. }
  158. static int hp_sdc_mlc_cts (hil_mlc *mlc) {
  159. struct hp_sdc_mlc_priv_s *priv;
  160. unsigned long flags;
  161. priv = mlc->priv;
  162. write_lock_irqsave(&(mlc->lock), flags);
  163. /* Try to down the semaphores -- they should be up. */
  164. if (down_trylock(&(mlc->isem))) {
  165. BUG();
  166. goto busy;
  167. }
  168. if (down_trylock(&(mlc->osem))) {
  169. BUG();
  170. up(&(mlc->isem));
  171. goto busy;
  172. }
  173. up(&(mlc->isem));
  174. up(&(mlc->osem));
  175. if (down_trylock(&(mlc->csem))) {
  176. if (priv->trans.act.semaphore != &(mlc->csem)) goto poll;
  177. goto busy;
  178. }
  179. if (!(priv->tseq[4] & HP_SDC_USE_LOOP)) goto done;
  180. poll:
  181. priv->trans.act.semaphore = &(mlc->csem);
  182. priv->trans.actidx = 0;
  183. priv->trans.idx = 1;
  184. priv->trans.endidx = 5;
  185. priv->tseq[0] =
  186. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
  187. priv->tseq[1] = HP_SDC_CMD_READ_USE;
  188. priv->tseq[2] = 1;
  189. priv->tseq[3] = 0;
  190. priv->tseq[4] = 0;
  191. hp_sdc_enqueue_transaction(&(priv->trans));
  192. busy:
  193. write_unlock_irqrestore(&(mlc->lock), flags);
  194. return 1;
  195. done:
  196. priv->trans.act.semaphore = &(mlc->osem);
  197. up(&(mlc->csem));
  198. write_unlock_irqrestore(&(mlc->lock), flags);
  199. return 0;
  200. }
  201. static void hp_sdc_mlc_out (hil_mlc *mlc) {
  202. struct hp_sdc_mlc_priv_s *priv;
  203. unsigned long flags;
  204. priv = mlc->priv;
  205. write_lock_irqsave(&(mlc->lock), flags);
  206. /* Try to down the semaphore -- it should be up. */
  207. if (down_trylock(&(mlc->osem))) {
  208. BUG();
  209. goto done;
  210. }
  211. if (mlc->opacket & HIL_DO_ALTER_CTRL) goto do_control;
  212. do_data:
  213. if (priv->emtestmode) {
  214. up(&(mlc->osem));
  215. goto done;
  216. }
  217. /* Shouldn't be sending commands when loop may be busy */
  218. if (down_trylock(&(mlc->csem))) {
  219. BUG();
  220. goto done;
  221. }
  222. up(&(mlc->csem));
  223. priv->trans.actidx = 0;
  224. priv->trans.idx = 1;
  225. priv->trans.act.semaphore = &(mlc->osem);
  226. priv->trans.endidx = 6;
  227. priv->tseq[0] =
  228. HP_SDC_ACT_DATAREG | HP_SDC_ACT_POSTCMD | HP_SDC_ACT_SEMAPHORE;
  229. priv->tseq[1] = 0x7;
  230. priv->tseq[2] =
  231. (mlc->opacket &
  232. (HIL_PKT_ADDR_MASK | HIL_PKT_CMD))
  233. >> HIL_PKT_ADDR_SHIFT;
  234. priv->tseq[3] =
  235. (mlc->opacket & HIL_PKT_DATA_MASK)
  236. >> HIL_PKT_DATA_SHIFT;
  237. priv->tseq[4] = 0; /* No timeout */
  238. if (priv->tseq[3] == HIL_CMD_DHR) priv->tseq[4] = 1;
  239. priv->tseq[5] = HP_SDC_CMD_DO_HIL;
  240. goto enqueue;
  241. do_control:
  242. priv->emtestmode = mlc->opacket & HIL_CTRL_TEST;
  243. if ((mlc->opacket & (HIL_CTRL_APE | HIL_CTRL_IPF)) == HIL_CTRL_APE) {
  244. BUG(); /* we cannot emulate this, it should not be used. */
  245. }
  246. if ((mlc->opacket & HIL_CTRL_ONLY) == HIL_CTRL_ONLY) goto control_only;
  247. if (mlc->opacket & HIL_CTRL_APE) {
  248. BUG(); /* Should not send command/data after engaging APE */
  249. goto done;
  250. }
  251. /* Disengaging APE this way would not be valid either since
  252. * the loop must be allowed to idle.
  253. *
  254. * So, it works out that we really never actually send control
  255. * and data when using SDC, we just send the data.
  256. */
  257. goto do_data;
  258. control_only:
  259. priv->trans.actidx = 0;
  260. priv->trans.idx = 1;
  261. priv->trans.act.semaphore = &(mlc->osem);
  262. priv->trans.endidx = 4;
  263. priv->tseq[0] =
  264. HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
  265. priv->tseq[1] = HP_SDC_CMD_SET_LPC;
  266. priv->tseq[2] = 1;
  267. // priv->tseq[3] = (mlc->ddc + 1) | HP_SDC_LPS_ACSUCC;
  268. priv->tseq[3] = 0;
  269. if (mlc->opacket & HIL_CTRL_APE) {
  270. priv->tseq[3] |= HP_SDC_LPC_APE_IPF;
  271. down_trylock(&(mlc->csem));
  272. }
  273. enqueue:
  274. hp_sdc_enqueue_transaction(&(priv->trans));
  275. done:
  276. write_unlock_irqrestore(&(mlc->lock), flags);
  277. }
  278. static int __init hp_sdc_mlc_init(void)
  279. {
  280. hil_mlc *mlc = &hp_sdc_mlc;
  281. printk(KERN_INFO PREFIX "Registering the System Domain Controller's HIL MLC.\n");
  282. hp_sdc_mlc_priv.emtestmode = 0;
  283. hp_sdc_mlc_priv.trans.seq = hp_sdc_mlc_priv.tseq;
  284. hp_sdc_mlc_priv.trans.act.semaphore = &(mlc->osem);
  285. hp_sdc_mlc_priv.got5x = 0;
  286. mlc->cts = &hp_sdc_mlc_cts;
  287. mlc->in = &hp_sdc_mlc_in;
  288. mlc->out = &hp_sdc_mlc_out;
  289. if (hil_mlc_register(mlc)) {
  290. printk(KERN_WARNING PREFIX "Failed to register MLC structure with hil_mlc\n");
  291. goto err0;
  292. }
  293. mlc->priv = &hp_sdc_mlc_priv;
  294. if (hp_sdc_request_hil_irq(&hp_sdc_mlc_isr)) {
  295. printk(KERN_WARNING PREFIX "Request for raw HIL ISR hook denied\n");
  296. goto err1;
  297. }
  298. return 0;
  299. err1:
  300. if (hil_mlc_unregister(mlc)) {
  301. printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
  302. "This is bad. Could cause an oops.\n");
  303. }
  304. err0:
  305. return -EBUSY;
  306. }
  307. static void __exit hp_sdc_mlc_exit(void)
  308. {
  309. hil_mlc *mlc = &hp_sdc_mlc;
  310. if (hp_sdc_release_hil_irq(&hp_sdc_mlc_isr)) {
  311. printk(KERN_ERR PREFIX "Failed to release the raw HIL ISR hook.\n"
  312. "This is bad. Could cause an oops.\n");
  313. }
  314. if (hil_mlc_unregister(mlc)) {
  315. printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
  316. "This is bad. Could cause an oops.\n");
  317. }
  318. }
  319. module_init(hp_sdc_mlc_init);
  320. module_exit(hp_sdc_mlc_exit);