hp_sdc.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * HP i8042-based System Device Controller driver.
  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. * System Device Controller Microprocessor Firmware Theory of Operation
  31. * for Part Number 1820-4784 Revision B. Dwg No. A-1820-4784-2
  32. * Helge Deller's original hilkbd.c port for PA-RISC.
  33. *
  34. *
  35. * Driver theory of operation:
  36. *
  37. * hp_sdc_put does all writing to the SDC. ISR can run on a different
  38. * CPU than hp_sdc_put, but only one CPU runs hp_sdc_put at a time
  39. * (it cannot really benefit from SMP anyway.) A tasket fit this perfectly.
  40. *
  41. * All data coming back from the SDC is sent via interrupt and can be read
  42. * fully in the ISR, so there are no latency/throughput problems there.
  43. * The problem is with output, due to the slow clock speed of the SDC
  44. * compared to the CPU. This should not be too horrible most of the time,
  45. * but if used with HIL devices that support the multibyte transfer command,
  46. * keeping outbound throughput flowing at the 6500KBps that the HIL is
  47. * capable of is more than can be done at HZ=100.
  48. *
  49. * Busy polling for IBF clear wastes CPU cycles and bus cycles. hp_sdc.ibf
  50. * is set to 0 when the IBF flag in the status register has cleared. ISR
  51. * may do this, and may also access the parts of queued transactions related
  52. * to reading data back from the SDC, but otherwise will not touch the
  53. * hp_sdc state. Whenever a register is written hp_sdc.ibf is set to 1.
  54. *
  55. * The i8042 write index and the values in the 4-byte input buffer
  56. * starting at 0x70 are kept track of in hp_sdc.wi, and .r7[], respectively,
  57. * to minimize the amount of IO needed to the SDC. However these values
  58. * do not need to be locked since they are only ever accessed by hp_sdc_put.
  59. *
  60. * A timer task schedules the tasklet once per second just to make
  61. * sure it doesn't freeze up and to allow for bad reads to time out.
  62. */
  63. #include <linux/hp_sdc.h>
  64. #include <linux/errno.h>
  65. #include <linux/init.h>
  66. #include <linux/module.h>
  67. #include <linux/ioport.h>
  68. #include <linux/time.h>
  69. #include <linux/slab.h>
  70. #include <linux/hil.h>
  71. #include <linux/semaphore.h>
  72. #include <asm/io.h>
  73. #include <asm/system.h>
  74. /* Machine-specific abstraction */
  75. #if defined(__hppa__)
  76. # include <asm/parisc-device.h>
  77. # define sdc_readb(p) gsc_readb(p)
  78. # define sdc_writeb(v,p) gsc_writeb((v),(p))
  79. #elif defined(__mc68000__)
  80. # include <asm/uaccess.h>
  81. # define sdc_readb(p) in_8(p)
  82. # define sdc_writeb(v,p) out_8((p),(v))
  83. #else
  84. # error "HIL is not supported on this platform"
  85. #endif
  86. #define PREFIX "HP SDC: "
  87. MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
  88. MODULE_DESCRIPTION("HP i8042-based SDC Driver");
  89. MODULE_LICENSE("Dual BSD/GPL");
  90. EXPORT_SYMBOL(hp_sdc_request_timer_irq);
  91. EXPORT_SYMBOL(hp_sdc_request_hil_irq);
  92. EXPORT_SYMBOL(hp_sdc_request_cooked_irq);
  93. EXPORT_SYMBOL(hp_sdc_release_timer_irq);
  94. EXPORT_SYMBOL(hp_sdc_release_hil_irq);
  95. EXPORT_SYMBOL(hp_sdc_release_cooked_irq);
  96. EXPORT_SYMBOL(__hp_sdc_enqueue_transaction);
  97. EXPORT_SYMBOL(hp_sdc_enqueue_transaction);
  98. EXPORT_SYMBOL(hp_sdc_dequeue_transaction);
  99. static hp_i8042_sdc hp_sdc; /* All driver state is kept in here. */
  100. /*************** primitives for use in any context *********************/
  101. static inline uint8_t hp_sdc_status_in8(void)
  102. {
  103. uint8_t status;
  104. unsigned long flags;
  105. write_lock_irqsave(&hp_sdc.ibf_lock, flags);
  106. status = sdc_readb(hp_sdc.status_io);
  107. if (!(status & HP_SDC_STATUS_IBF))
  108. hp_sdc.ibf = 0;
  109. write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
  110. return status;
  111. }
  112. static inline uint8_t hp_sdc_data_in8(void)
  113. {
  114. return sdc_readb(hp_sdc.data_io);
  115. }
  116. static inline void hp_sdc_status_out8(uint8_t val)
  117. {
  118. unsigned long flags;
  119. write_lock_irqsave(&hp_sdc.ibf_lock, flags);
  120. hp_sdc.ibf = 1;
  121. if ((val & 0xf0) == 0xe0)
  122. hp_sdc.wi = 0xff;
  123. sdc_writeb(val, hp_sdc.status_io);
  124. write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
  125. }
  126. static inline void hp_sdc_data_out8(uint8_t val)
  127. {
  128. unsigned long flags;
  129. write_lock_irqsave(&hp_sdc.ibf_lock, flags);
  130. hp_sdc.ibf = 1;
  131. sdc_writeb(val, hp_sdc.data_io);
  132. write_unlock_irqrestore(&hp_sdc.ibf_lock, flags);
  133. }
  134. /* Care must be taken to only invoke hp_sdc_spin_ibf when
  135. * absolutely needed, or in rarely invoked subroutines.
  136. * Not only does it waste CPU cycles, it also wastes bus cycles.
  137. */
  138. static inline void hp_sdc_spin_ibf(void)
  139. {
  140. unsigned long flags;
  141. rwlock_t *lock;
  142. lock = &hp_sdc.ibf_lock;
  143. read_lock_irqsave(lock, flags);
  144. if (!hp_sdc.ibf) {
  145. read_unlock_irqrestore(lock, flags);
  146. return;
  147. }
  148. read_unlock(lock);
  149. write_lock(lock);
  150. while (sdc_readb(hp_sdc.status_io) & HP_SDC_STATUS_IBF)
  151. { }
  152. hp_sdc.ibf = 0;
  153. write_unlock_irqrestore(lock, flags);
  154. }
  155. /************************ Interrupt context functions ************************/
  156. static void hp_sdc_take(int irq, void *dev_id, uint8_t status, uint8_t data)
  157. {
  158. hp_sdc_transaction *curr;
  159. read_lock(&hp_sdc.rtq_lock);
  160. if (hp_sdc.rcurr < 0) {
  161. read_unlock(&hp_sdc.rtq_lock);
  162. return;
  163. }
  164. curr = hp_sdc.tq[hp_sdc.rcurr];
  165. read_unlock(&hp_sdc.rtq_lock);
  166. curr->seq[curr->idx++] = status;
  167. curr->seq[curr->idx++] = data;
  168. hp_sdc.rqty -= 2;
  169. do_gettimeofday(&hp_sdc.rtv);
  170. if (hp_sdc.rqty <= 0) {
  171. /* All data has been gathered. */
  172. if (curr->seq[curr->actidx] & HP_SDC_ACT_SEMAPHORE)
  173. if (curr->act.semaphore)
  174. up(curr->act.semaphore);
  175. if (curr->seq[curr->actidx] & HP_SDC_ACT_CALLBACK)
  176. if (curr->act.irqhook)
  177. curr->act.irqhook(irq, dev_id, status, data);
  178. curr->actidx = curr->idx;
  179. curr->idx++;
  180. /* Return control of this transaction */
  181. write_lock(&hp_sdc.rtq_lock);
  182. hp_sdc.rcurr = -1;
  183. hp_sdc.rqty = 0;
  184. write_unlock(&hp_sdc.rtq_lock);
  185. tasklet_schedule(&hp_sdc.task);
  186. }
  187. }
  188. static irqreturn_t hp_sdc_isr(int irq, void *dev_id)
  189. {
  190. uint8_t status, data;
  191. status = hp_sdc_status_in8();
  192. /* Read data unconditionally to advance i8042. */
  193. data = hp_sdc_data_in8();
  194. /* For now we are ignoring these until we get the SDC to behave. */
  195. if (((status & 0xf1) == 0x51) && data == 0x82)
  196. return IRQ_HANDLED;
  197. switch (status & HP_SDC_STATUS_IRQMASK) {
  198. case 0: /* This case is not documented. */
  199. break;
  200. case HP_SDC_STATUS_USERTIMER:
  201. case HP_SDC_STATUS_PERIODIC:
  202. case HP_SDC_STATUS_TIMER:
  203. read_lock(&hp_sdc.hook_lock);
  204. if (hp_sdc.timer != NULL)
  205. hp_sdc.timer(irq, dev_id, status, data);
  206. read_unlock(&hp_sdc.hook_lock);
  207. break;
  208. case HP_SDC_STATUS_REG:
  209. hp_sdc_take(irq, dev_id, status, data);
  210. break;
  211. case HP_SDC_STATUS_HILCMD:
  212. case HP_SDC_STATUS_HILDATA:
  213. read_lock(&hp_sdc.hook_lock);
  214. if (hp_sdc.hil != NULL)
  215. hp_sdc.hil(irq, dev_id, status, data);
  216. read_unlock(&hp_sdc.hook_lock);
  217. break;
  218. case HP_SDC_STATUS_PUP:
  219. read_lock(&hp_sdc.hook_lock);
  220. if (hp_sdc.pup != NULL)
  221. hp_sdc.pup(irq, dev_id, status, data);
  222. else
  223. printk(KERN_INFO PREFIX "HP SDC reports successful PUP.\n");
  224. read_unlock(&hp_sdc.hook_lock);
  225. break;
  226. default:
  227. read_lock(&hp_sdc.hook_lock);
  228. if (hp_sdc.cooked != NULL)
  229. hp_sdc.cooked(irq, dev_id, status, data);
  230. read_unlock(&hp_sdc.hook_lock);
  231. break;
  232. }
  233. return IRQ_HANDLED;
  234. }
  235. static irqreturn_t hp_sdc_nmisr(int irq, void *dev_id)
  236. {
  237. int status;
  238. status = hp_sdc_status_in8();
  239. printk(KERN_WARNING PREFIX "NMI !\n");
  240. #if 0
  241. if (status & HP_SDC_NMISTATUS_FHS) {
  242. read_lock(&hp_sdc.hook_lock);
  243. if (hp_sdc.timer != NULL)
  244. hp_sdc.timer(irq, dev_id, status, 0);
  245. read_unlock(&hp_sdc.hook_lock);
  246. } else {
  247. /* TODO: pass this on to the HIL handler, or do SAK here? */
  248. printk(KERN_WARNING PREFIX "HIL NMI\n");
  249. }
  250. #endif
  251. return IRQ_HANDLED;
  252. }
  253. /***************** Kernel (tasklet) context functions ****************/
  254. unsigned long hp_sdc_put(void);
  255. static void hp_sdc_tasklet(unsigned long foo)
  256. {
  257. write_lock_irq(&hp_sdc.rtq_lock);
  258. if (hp_sdc.rcurr >= 0) {
  259. struct timeval tv;
  260. do_gettimeofday(&tv);
  261. if (tv.tv_sec > hp_sdc.rtv.tv_sec)
  262. tv.tv_usec += USEC_PER_SEC;
  263. if (tv.tv_usec - hp_sdc.rtv.tv_usec > HP_SDC_MAX_REG_DELAY) {
  264. hp_sdc_transaction *curr;
  265. uint8_t tmp;
  266. curr = hp_sdc.tq[hp_sdc.rcurr];
  267. /* If this turns out to be a normal failure mode
  268. * we'll need to figure out a way to communicate
  269. * it back to the application. and be less verbose.
  270. */
  271. printk(KERN_WARNING PREFIX "read timeout (%ius)!\n",
  272. tv.tv_usec - hp_sdc.rtv.tv_usec);
  273. curr->idx += hp_sdc.rqty;
  274. hp_sdc.rqty = 0;
  275. tmp = curr->seq[curr->actidx];
  276. curr->seq[curr->actidx] |= HP_SDC_ACT_DEAD;
  277. if (tmp & HP_SDC_ACT_SEMAPHORE)
  278. if (curr->act.semaphore)
  279. up(curr->act.semaphore);
  280. if (tmp & HP_SDC_ACT_CALLBACK) {
  281. /* Note this means that irqhooks may be called
  282. * in tasklet/bh context.
  283. */
  284. if (curr->act.irqhook)
  285. curr->act.irqhook(0, NULL, 0, 0);
  286. }
  287. curr->actidx = curr->idx;
  288. curr->idx++;
  289. hp_sdc.rcurr = -1;
  290. }
  291. }
  292. write_unlock_irq(&hp_sdc.rtq_lock);
  293. hp_sdc_put();
  294. }
  295. unsigned long hp_sdc_put(void)
  296. {
  297. hp_sdc_transaction *curr;
  298. uint8_t act;
  299. int idx, curridx;
  300. int limit = 0;
  301. write_lock(&hp_sdc.lock);
  302. /* If i8042 buffers are full, we cannot do anything that
  303. requires output, so we skip to the administrativa. */
  304. if (hp_sdc.ibf) {
  305. hp_sdc_status_in8();
  306. if (hp_sdc.ibf)
  307. goto finish;
  308. }
  309. anew:
  310. /* See if we are in the middle of a sequence. */
  311. if (hp_sdc.wcurr < 0)
  312. hp_sdc.wcurr = 0;
  313. read_lock_irq(&hp_sdc.rtq_lock);
  314. if (hp_sdc.rcurr == hp_sdc.wcurr)
  315. hp_sdc.wcurr++;
  316. read_unlock_irq(&hp_sdc.rtq_lock);
  317. if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
  318. hp_sdc.wcurr = 0;
  319. curridx = hp_sdc.wcurr;
  320. if (hp_sdc.tq[curridx] != NULL)
  321. goto start;
  322. while (++curridx != hp_sdc.wcurr) {
  323. if (curridx >= HP_SDC_QUEUE_LEN) {
  324. curridx = -1; /* Wrap to top */
  325. continue;
  326. }
  327. read_lock_irq(&hp_sdc.rtq_lock);
  328. if (hp_sdc.rcurr == curridx) {
  329. read_unlock_irq(&hp_sdc.rtq_lock);
  330. continue;
  331. }
  332. read_unlock_irq(&hp_sdc.rtq_lock);
  333. if (hp_sdc.tq[curridx] != NULL)
  334. break; /* Found one. */
  335. }
  336. if (curridx == hp_sdc.wcurr) { /* There's nothing queued to do. */
  337. curridx = -1;
  338. }
  339. hp_sdc.wcurr = curridx;
  340. start:
  341. /* Check to see if the interrupt mask needs to be set. */
  342. if (hp_sdc.set_im) {
  343. hp_sdc_status_out8(hp_sdc.im | HP_SDC_CMD_SET_IM);
  344. hp_sdc.set_im = 0;
  345. goto finish;
  346. }
  347. if (hp_sdc.wcurr == -1)
  348. goto done;
  349. curr = hp_sdc.tq[curridx];
  350. idx = curr->actidx;
  351. if (curr->actidx >= curr->endidx) {
  352. hp_sdc.tq[curridx] = NULL;
  353. /* Interleave outbound data between the transactions. */
  354. hp_sdc.wcurr++;
  355. if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
  356. hp_sdc.wcurr = 0;
  357. goto finish;
  358. }
  359. act = curr->seq[idx];
  360. idx++;
  361. if (curr->idx >= curr->endidx) {
  362. if (act & HP_SDC_ACT_DEALLOC)
  363. kfree(curr);
  364. hp_sdc.tq[curridx] = NULL;
  365. /* Interleave outbound data between the transactions. */
  366. hp_sdc.wcurr++;
  367. if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
  368. hp_sdc.wcurr = 0;
  369. goto finish;
  370. }
  371. while (act & HP_SDC_ACT_PRECMD) {
  372. if (curr->idx != idx) {
  373. idx++;
  374. act &= ~HP_SDC_ACT_PRECMD;
  375. break;
  376. }
  377. hp_sdc_status_out8(curr->seq[idx]);
  378. curr->idx++;
  379. /* act finished? */
  380. if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_PRECMD)
  381. goto actdone;
  382. /* skip quantity field if data-out sequence follows. */
  383. if (act & HP_SDC_ACT_DATAOUT)
  384. curr->idx++;
  385. goto finish;
  386. }
  387. if (act & HP_SDC_ACT_DATAOUT) {
  388. int qty;
  389. qty = curr->seq[idx];
  390. idx++;
  391. if (curr->idx - idx < qty) {
  392. hp_sdc_data_out8(curr->seq[curr->idx]);
  393. curr->idx++;
  394. /* act finished? */
  395. if (curr->idx - idx >= qty &&
  396. (act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAOUT)
  397. goto actdone;
  398. goto finish;
  399. }
  400. idx += qty;
  401. act &= ~HP_SDC_ACT_DATAOUT;
  402. } else
  403. while (act & HP_SDC_ACT_DATAREG) {
  404. int mask;
  405. uint8_t w7[4];
  406. mask = curr->seq[idx];
  407. if (idx != curr->idx) {
  408. idx++;
  409. idx += !!(mask & 1);
  410. idx += !!(mask & 2);
  411. idx += !!(mask & 4);
  412. idx += !!(mask & 8);
  413. act &= ~HP_SDC_ACT_DATAREG;
  414. break;
  415. }
  416. w7[0] = (mask & 1) ? curr->seq[++idx] : hp_sdc.r7[0];
  417. w7[1] = (mask & 2) ? curr->seq[++idx] : hp_sdc.r7[1];
  418. w7[2] = (mask & 4) ? curr->seq[++idx] : hp_sdc.r7[2];
  419. w7[3] = (mask & 8) ? curr->seq[++idx] : hp_sdc.r7[3];
  420. if (hp_sdc.wi > 0x73 || hp_sdc.wi < 0x70 ||
  421. w7[hp_sdc.wi - 0x70] == hp_sdc.r7[hp_sdc.wi - 0x70]) {
  422. int i = 0;
  423. /* Need to point the write index register */
  424. while (i < 4 && w7[i] == hp_sdc.r7[i])
  425. i++;
  426. if (i < 4) {
  427. hp_sdc_status_out8(HP_SDC_CMD_SET_D0 + i);
  428. hp_sdc.wi = 0x70 + i;
  429. goto finish;
  430. }
  431. idx++;
  432. if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAREG)
  433. goto actdone;
  434. curr->idx = idx;
  435. act &= ~HP_SDC_ACT_DATAREG;
  436. break;
  437. }
  438. hp_sdc_data_out8(w7[hp_sdc.wi - 0x70]);
  439. hp_sdc.r7[hp_sdc.wi - 0x70] = w7[hp_sdc.wi - 0x70];
  440. hp_sdc.wi++; /* write index register autoincrements */
  441. {
  442. int i = 0;
  443. while ((i < 4) && w7[i] == hp_sdc.r7[i])
  444. i++;
  445. if (i >= 4) {
  446. curr->idx = idx + 1;
  447. if ((act & HP_SDC_ACT_DURING) ==
  448. HP_SDC_ACT_DATAREG)
  449. goto actdone;
  450. }
  451. }
  452. goto finish;
  453. }
  454. /* We don't go any further in the command if there is a pending read,
  455. because we don't want interleaved results. */
  456. read_lock_irq(&hp_sdc.rtq_lock);
  457. if (hp_sdc.rcurr >= 0) {
  458. read_unlock_irq(&hp_sdc.rtq_lock);
  459. goto finish;
  460. }
  461. read_unlock_irq(&hp_sdc.rtq_lock);
  462. if (act & HP_SDC_ACT_POSTCMD) {
  463. uint8_t postcmd;
  464. /* curr->idx should == idx at this point. */
  465. postcmd = curr->seq[idx];
  466. curr->idx++;
  467. if (act & HP_SDC_ACT_DATAIN) {
  468. /* Start a new read */
  469. hp_sdc.rqty = curr->seq[curr->idx];
  470. do_gettimeofday(&hp_sdc.rtv);
  471. curr->idx++;
  472. /* Still need to lock here in case of spurious irq. */
  473. write_lock_irq(&hp_sdc.rtq_lock);
  474. hp_sdc.rcurr = curridx;
  475. write_unlock_irq(&hp_sdc.rtq_lock);
  476. hp_sdc_status_out8(postcmd);
  477. goto finish;
  478. }
  479. hp_sdc_status_out8(postcmd);
  480. goto actdone;
  481. }
  482. actdone:
  483. if (act & HP_SDC_ACT_SEMAPHORE)
  484. up(curr->act.semaphore);
  485. else if (act & HP_SDC_ACT_CALLBACK)
  486. curr->act.irqhook(0,NULL,0,0);
  487. if (curr->idx >= curr->endidx) { /* This transaction is over. */
  488. if (act & HP_SDC_ACT_DEALLOC)
  489. kfree(curr);
  490. hp_sdc.tq[curridx] = NULL;
  491. } else {
  492. curr->actidx = idx + 1;
  493. curr->idx = idx + 2;
  494. }
  495. /* Interleave outbound data between the transactions. */
  496. hp_sdc.wcurr++;
  497. if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
  498. hp_sdc.wcurr = 0;
  499. finish:
  500. /* If by some quirk IBF has cleared and our ISR has run to
  501. see that that has happened, do it all again. */
  502. if (!hp_sdc.ibf && limit++ < 20)
  503. goto anew;
  504. done:
  505. if (hp_sdc.wcurr >= 0)
  506. tasklet_schedule(&hp_sdc.task);
  507. write_unlock(&hp_sdc.lock);
  508. return 0;
  509. }
  510. /******* Functions called in either user or kernel context ****/
  511. int __hp_sdc_enqueue_transaction(hp_sdc_transaction *this)
  512. {
  513. int i;
  514. if (this == NULL) {
  515. BUG();
  516. return -EINVAL;
  517. }
  518. /* Can't have same transaction on queue twice */
  519. for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
  520. if (hp_sdc.tq[i] == this)
  521. goto fail;
  522. this->actidx = 0;
  523. this->idx = 1;
  524. /* Search for empty slot */
  525. for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
  526. if (hp_sdc.tq[i] == NULL) {
  527. hp_sdc.tq[i] = this;
  528. tasklet_schedule(&hp_sdc.task);
  529. return 0;
  530. }
  531. printk(KERN_WARNING PREFIX "No free slot to add transaction.\n");
  532. return -EBUSY;
  533. fail:
  534. printk(KERN_WARNING PREFIX "Transaction add failed: transaction already queued?\n");
  535. return -EINVAL;
  536. }
  537. int hp_sdc_enqueue_transaction(hp_sdc_transaction *this) {
  538. unsigned long flags;
  539. int ret;
  540. write_lock_irqsave(&hp_sdc.lock, flags);
  541. ret = __hp_sdc_enqueue_transaction(this);
  542. write_unlock_irqrestore(&hp_sdc.lock,flags);
  543. return ret;
  544. }
  545. int hp_sdc_dequeue_transaction(hp_sdc_transaction *this)
  546. {
  547. unsigned long flags;
  548. int i;
  549. write_lock_irqsave(&hp_sdc.lock, flags);
  550. /* TODO: don't remove it if it's not done. */
  551. for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
  552. if (hp_sdc.tq[i] == this)
  553. hp_sdc.tq[i] = NULL;
  554. write_unlock_irqrestore(&hp_sdc.lock, flags);
  555. return 0;
  556. }
  557. /********************** User context functions **************************/
  558. int hp_sdc_request_timer_irq(hp_sdc_irqhook *callback)
  559. {
  560. if (callback == NULL || hp_sdc.dev == NULL)
  561. return -EINVAL;
  562. write_lock_irq(&hp_sdc.hook_lock);
  563. if (hp_sdc.timer != NULL) {
  564. write_unlock_irq(&hp_sdc.hook_lock);
  565. return -EBUSY;
  566. }
  567. hp_sdc.timer = callback;
  568. /* Enable interrupts from the timers */
  569. hp_sdc.im &= ~HP_SDC_IM_FH;
  570. hp_sdc.im &= ~HP_SDC_IM_PT;
  571. hp_sdc.im &= ~HP_SDC_IM_TIMERS;
  572. hp_sdc.set_im = 1;
  573. write_unlock_irq(&hp_sdc.hook_lock);
  574. tasklet_schedule(&hp_sdc.task);
  575. return 0;
  576. }
  577. int hp_sdc_request_hil_irq(hp_sdc_irqhook *callback)
  578. {
  579. if (callback == NULL || hp_sdc.dev == NULL)
  580. return -EINVAL;
  581. write_lock_irq(&hp_sdc.hook_lock);
  582. if (hp_sdc.hil != NULL) {
  583. write_unlock_irq(&hp_sdc.hook_lock);
  584. return -EBUSY;
  585. }
  586. hp_sdc.hil = callback;
  587. hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
  588. hp_sdc.set_im = 1;
  589. write_unlock_irq(&hp_sdc.hook_lock);
  590. tasklet_schedule(&hp_sdc.task);
  591. return 0;
  592. }
  593. int hp_sdc_request_cooked_irq(hp_sdc_irqhook *callback)
  594. {
  595. if (callback == NULL || hp_sdc.dev == NULL)
  596. return -EINVAL;
  597. write_lock_irq(&hp_sdc.hook_lock);
  598. if (hp_sdc.cooked != NULL) {
  599. write_unlock_irq(&hp_sdc.hook_lock);
  600. return -EBUSY;
  601. }
  602. /* Enable interrupts from the HIL MLC */
  603. hp_sdc.cooked = callback;
  604. hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
  605. hp_sdc.set_im = 1;
  606. write_unlock_irq(&hp_sdc.hook_lock);
  607. tasklet_schedule(&hp_sdc.task);
  608. return 0;
  609. }
  610. int hp_sdc_release_timer_irq(hp_sdc_irqhook *callback)
  611. {
  612. write_lock_irq(&hp_sdc.hook_lock);
  613. if ((callback != hp_sdc.timer) ||
  614. (hp_sdc.timer == NULL)) {
  615. write_unlock_irq(&hp_sdc.hook_lock);
  616. return -EINVAL;
  617. }
  618. /* Disable interrupts from the timers */
  619. hp_sdc.timer = NULL;
  620. hp_sdc.im |= HP_SDC_IM_TIMERS;
  621. hp_sdc.im |= HP_SDC_IM_FH;
  622. hp_sdc.im |= HP_SDC_IM_PT;
  623. hp_sdc.set_im = 1;
  624. write_unlock_irq(&hp_sdc.hook_lock);
  625. tasklet_schedule(&hp_sdc.task);
  626. return 0;
  627. }
  628. int hp_sdc_release_hil_irq(hp_sdc_irqhook *callback)
  629. {
  630. write_lock_irq(&hp_sdc.hook_lock);
  631. if ((callback != hp_sdc.hil) ||
  632. (hp_sdc.hil == NULL)) {
  633. write_unlock_irq(&hp_sdc.hook_lock);
  634. return -EINVAL;
  635. }
  636. hp_sdc.hil = NULL;
  637. /* Disable interrupts from HIL only if there is no cooked driver. */
  638. if(hp_sdc.cooked == NULL) {
  639. hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
  640. hp_sdc.set_im = 1;
  641. }
  642. write_unlock_irq(&hp_sdc.hook_lock);
  643. tasklet_schedule(&hp_sdc.task);
  644. return 0;
  645. }
  646. int hp_sdc_release_cooked_irq(hp_sdc_irqhook *callback)
  647. {
  648. write_lock_irq(&hp_sdc.hook_lock);
  649. if ((callback != hp_sdc.cooked) ||
  650. (hp_sdc.cooked == NULL)) {
  651. write_unlock_irq(&hp_sdc.hook_lock);
  652. return -EINVAL;
  653. }
  654. hp_sdc.cooked = NULL;
  655. /* Disable interrupts from HIL only if there is no raw HIL driver. */
  656. if(hp_sdc.hil == NULL) {
  657. hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
  658. hp_sdc.set_im = 1;
  659. }
  660. write_unlock_irq(&hp_sdc.hook_lock);
  661. tasklet_schedule(&hp_sdc.task);
  662. return 0;
  663. }
  664. /************************* Keepalive timer task *********************/
  665. void hp_sdc_kicker (unsigned long data)
  666. {
  667. tasklet_schedule(&hp_sdc.task);
  668. /* Re-insert the periodic task. */
  669. mod_timer(&hp_sdc.kicker, jiffies + HZ);
  670. }
  671. /************************** Module Initialization ***************************/
  672. #if defined(__hppa__)
  673. static const struct parisc_device_id hp_sdc_tbl[] = {
  674. {
  675. .hw_type = HPHW_FIO,
  676. .hversion_rev = HVERSION_REV_ANY_ID,
  677. .hversion = HVERSION_ANY_ID,
  678. .sversion = 0x73,
  679. },
  680. { 0, }
  681. };
  682. MODULE_DEVICE_TABLE(parisc, hp_sdc_tbl);
  683. static int __init hp_sdc_init_hppa(struct parisc_device *d);
  684. static struct parisc_driver hp_sdc_driver = {
  685. .name = "hp_sdc",
  686. .id_table = hp_sdc_tbl,
  687. .probe = hp_sdc_init_hppa,
  688. };
  689. #endif /* __hppa__ */
  690. static int __init hp_sdc_init(void)
  691. {
  692. char *errstr;
  693. hp_sdc_transaction t_sync;
  694. uint8_t ts_sync[6];
  695. struct semaphore s_sync;
  696. rwlock_init(&hp_sdc.lock);
  697. rwlock_init(&hp_sdc.ibf_lock);
  698. rwlock_init(&hp_sdc.rtq_lock);
  699. rwlock_init(&hp_sdc.hook_lock);
  700. hp_sdc.timer = NULL;
  701. hp_sdc.hil = NULL;
  702. hp_sdc.pup = NULL;
  703. hp_sdc.cooked = NULL;
  704. hp_sdc.im = HP_SDC_IM_MASK; /* Mask maskable irqs */
  705. hp_sdc.set_im = 1;
  706. hp_sdc.wi = 0xff;
  707. hp_sdc.r7[0] = 0xff;
  708. hp_sdc.r7[1] = 0xff;
  709. hp_sdc.r7[2] = 0xff;
  710. hp_sdc.r7[3] = 0xff;
  711. hp_sdc.ibf = 1;
  712. memset(&hp_sdc.tq, 0, sizeof(hp_sdc.tq));
  713. hp_sdc.wcurr = -1;
  714. hp_sdc.rcurr = -1;
  715. hp_sdc.rqty = 0;
  716. hp_sdc.dev_err = -ENODEV;
  717. errstr = "IO not found for";
  718. if (!hp_sdc.base_io)
  719. goto err0;
  720. errstr = "IRQ not found for";
  721. if (!hp_sdc.irq)
  722. goto err0;
  723. hp_sdc.dev_err = -EBUSY;
  724. #if defined(__hppa__)
  725. errstr = "IO not available for";
  726. if (request_region(hp_sdc.data_io, 2, hp_sdc_driver.name))
  727. goto err0;
  728. #endif
  729. errstr = "IRQ not available for";
  730. if (request_irq(hp_sdc.irq, &hp_sdc_isr, IRQF_SHARED|IRQF_SAMPLE_RANDOM,
  731. "HP SDC", &hp_sdc))
  732. goto err1;
  733. errstr = "NMI not available for";
  734. if (request_irq(hp_sdc.nmi, &hp_sdc_nmisr, IRQF_SHARED,
  735. "HP SDC NMI", &hp_sdc))
  736. goto err2;
  737. printk(KERN_INFO PREFIX "HP SDC at 0x%p, IRQ %d (NMI IRQ %d)\n",
  738. (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
  739. hp_sdc_status_in8();
  740. hp_sdc_data_in8();
  741. tasklet_init(&hp_sdc.task, hp_sdc_tasklet, 0);
  742. /* Sync the output buffer registers, thus scheduling hp_sdc_tasklet. */
  743. t_sync.actidx = 0;
  744. t_sync.idx = 1;
  745. t_sync.endidx = 6;
  746. t_sync.seq = ts_sync;
  747. ts_sync[0] = HP_SDC_ACT_DATAREG | HP_SDC_ACT_SEMAPHORE;
  748. ts_sync[1] = 0x0f;
  749. ts_sync[2] = ts_sync[3] = ts_sync[4] = ts_sync[5] = 0;
  750. t_sync.act.semaphore = &s_sync;
  751. init_MUTEX_LOCKED(&s_sync);
  752. hp_sdc_enqueue_transaction(&t_sync);
  753. down(&s_sync); /* Wait for t_sync to complete */
  754. /* Create the keepalive task */
  755. init_timer(&hp_sdc.kicker);
  756. hp_sdc.kicker.expires = jiffies + HZ;
  757. hp_sdc.kicker.function = &hp_sdc_kicker;
  758. add_timer(&hp_sdc.kicker);
  759. hp_sdc.dev_err = 0;
  760. return 0;
  761. err2:
  762. free_irq(hp_sdc.irq, &hp_sdc);
  763. err1:
  764. release_region(hp_sdc.data_io, 2);
  765. err0:
  766. printk(KERN_WARNING PREFIX ": %s SDC IO=0x%p IRQ=0x%x NMI=0x%x\n",
  767. errstr, (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
  768. hp_sdc.dev = NULL;
  769. return hp_sdc.dev_err;
  770. }
  771. #if defined(__hppa__)
  772. static int __init hp_sdc_init_hppa(struct parisc_device *d)
  773. {
  774. if (!d)
  775. return 1;
  776. if (hp_sdc.dev != NULL)
  777. return 1; /* We only expect one SDC */
  778. hp_sdc.dev = d;
  779. hp_sdc.irq = d->irq;
  780. hp_sdc.nmi = d->aux_irq;
  781. hp_sdc.base_io = d->hpa.start;
  782. hp_sdc.data_io = d->hpa.start + 0x800;
  783. hp_sdc.status_io = d->hpa.start + 0x801;
  784. return hp_sdc_init();
  785. }
  786. #endif /* __hppa__ */
  787. static void hp_sdc_exit(void)
  788. {
  789. write_lock_irq(&hp_sdc.lock);
  790. /* Turn off all maskable "sub-function" irq's. */
  791. hp_sdc_spin_ibf();
  792. sdc_writeb(HP_SDC_CMD_SET_IM | HP_SDC_IM_MASK, hp_sdc.status_io);
  793. /* Wait until we know this has been processed by the i8042 */
  794. hp_sdc_spin_ibf();
  795. free_irq(hp_sdc.nmi, &hp_sdc);
  796. free_irq(hp_sdc.irq, &hp_sdc);
  797. write_unlock_irq(&hp_sdc.lock);
  798. del_timer(&hp_sdc.kicker);
  799. tasklet_kill(&hp_sdc.task);
  800. #if defined(__hppa__)
  801. if (unregister_parisc_driver(&hp_sdc_driver))
  802. printk(KERN_WARNING PREFIX "Error unregistering HP SDC");
  803. #endif
  804. }
  805. static int __init hp_sdc_register(void)
  806. {
  807. hp_sdc_transaction tq_init;
  808. uint8_t tq_init_seq[5];
  809. struct semaphore tq_init_sem;
  810. #if defined(__mc68000__)
  811. mm_segment_t fs;
  812. unsigned char i;
  813. #endif
  814. hp_sdc.dev = NULL;
  815. hp_sdc.dev_err = 0;
  816. #if defined(__hppa__)
  817. if (register_parisc_driver(&hp_sdc_driver)) {
  818. printk(KERN_WARNING PREFIX "Error registering SDC with system bus tree.\n");
  819. return -ENODEV;
  820. }
  821. #elif defined(__mc68000__)
  822. if (!MACH_IS_HP300)
  823. return -ENODEV;
  824. hp_sdc.irq = 1;
  825. hp_sdc.nmi = 7;
  826. hp_sdc.base_io = (unsigned long) 0xf0428000;
  827. hp_sdc.data_io = (unsigned long) hp_sdc.base_io + 1;
  828. hp_sdc.status_io = (unsigned long) hp_sdc.base_io + 3;
  829. fs = get_fs();
  830. set_fs(KERNEL_DS);
  831. if (!get_user(i, (unsigned char *)hp_sdc.data_io))
  832. hp_sdc.dev = (void *)1;
  833. set_fs(fs);
  834. hp_sdc.dev_err = hp_sdc_init();
  835. #endif
  836. if (hp_sdc.dev == NULL) {
  837. printk(KERN_WARNING PREFIX "No SDC found.\n");
  838. return hp_sdc.dev_err;
  839. }
  840. init_MUTEX_LOCKED(&tq_init_sem);
  841. tq_init.actidx = 0;
  842. tq_init.idx = 1;
  843. tq_init.endidx = 5;
  844. tq_init.seq = tq_init_seq;
  845. tq_init.act.semaphore = &tq_init_sem;
  846. tq_init_seq[0] =
  847. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
  848. tq_init_seq[1] = HP_SDC_CMD_READ_KCC;
  849. tq_init_seq[2] = 1;
  850. tq_init_seq[3] = 0;
  851. tq_init_seq[4] = 0;
  852. hp_sdc_enqueue_transaction(&tq_init);
  853. down(&tq_init_sem);
  854. up(&tq_init_sem);
  855. if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
  856. printk(KERN_WARNING PREFIX "Error reading config byte.\n");
  857. hp_sdc_exit();
  858. return -ENODEV;
  859. }
  860. hp_sdc.r11 = tq_init_seq[4];
  861. if (hp_sdc.r11 & HP_SDC_CFG_NEW) {
  862. const char *str;
  863. printk(KERN_INFO PREFIX "New style SDC\n");
  864. tq_init_seq[1] = HP_SDC_CMD_READ_XTD;
  865. tq_init.actidx = 0;
  866. tq_init.idx = 1;
  867. down(&tq_init_sem);
  868. hp_sdc_enqueue_transaction(&tq_init);
  869. down(&tq_init_sem);
  870. up(&tq_init_sem);
  871. if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
  872. printk(KERN_WARNING PREFIX "Error reading extended config byte.\n");
  873. return -ENODEV;
  874. }
  875. hp_sdc.r7e = tq_init_seq[4];
  876. HP_SDC_XTD_REV_STRINGS(hp_sdc.r7e & HP_SDC_XTD_REV, str)
  877. printk(KERN_INFO PREFIX "Revision: %s\n", str);
  878. if (hp_sdc.r7e & HP_SDC_XTD_BEEPER)
  879. printk(KERN_INFO PREFIX "TI SN76494 beeper present\n");
  880. if (hp_sdc.r7e & HP_SDC_XTD_BBRTC)
  881. printk(KERN_INFO PREFIX "OKI MSM-58321 BBRTC present\n");
  882. printk(KERN_INFO PREFIX "Spunking the self test register to force PUP "
  883. "on next firmware reset.\n");
  884. tq_init_seq[0] = HP_SDC_ACT_PRECMD |
  885. HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
  886. tq_init_seq[1] = HP_SDC_CMD_SET_STR;
  887. tq_init_seq[2] = 1;
  888. tq_init_seq[3] = 0;
  889. tq_init.actidx = 0;
  890. tq_init.idx = 1;
  891. tq_init.endidx = 4;
  892. down(&tq_init_sem);
  893. hp_sdc_enqueue_transaction(&tq_init);
  894. down(&tq_init_sem);
  895. up(&tq_init_sem);
  896. } else
  897. printk(KERN_INFO PREFIX "Old style SDC (1820-%s).\n",
  898. (hp_sdc.r11 & HP_SDC_CFG_REV) ? "3300" : "2564/3087");
  899. return 0;
  900. }
  901. module_init(hp_sdc_register);
  902. module_exit(hp_sdc_exit);
  903. /* Timing notes: These measurements taken on my 64MHz 7100-LC (715/64)
  904. * cycles cycles-adj time
  905. * between two consecutive mfctl(16)'s: 4 n/a 63ns
  906. * hp_sdc_spin_ibf when idle: 119 115 1.7us
  907. * gsc_writeb status register: 83 79 1.2us
  908. * IBF to clear after sending SET_IM: 6204 6006 93us
  909. * IBF to clear after sending LOAD_RT: 4467 4352 68us
  910. * IBF to clear after sending two LOAD_RTs: 18974 18859 295us
  911. * READ_T1, read status/data, IRQ, call handler: 35564 n/a 556us
  912. * cmd to ~IBF READ_T1 2nd time right after: 5158403 n/a 81ms
  913. * between IRQ received and ~IBF for above: 2578877 n/a 40ms
  914. *
  915. * Performance stats after a run of this module configuring HIL and
  916. * receiving a few mouse events:
  917. *
  918. * status in8 282508 cycles 7128 calls
  919. * status out8 8404 cycles 341 calls
  920. * data out8 1734 cycles 78 calls
  921. * isr 174324 cycles 617 calls (includes take)
  922. * take 1241 cycles 2 calls
  923. * put 1411504 cycles 6937 calls
  924. * task 1655209 cycles 6937 calls (includes put)
  925. *
  926. */