hp_sdc_mlc.c 9.7 KB

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