hp_sdc.c 28 KB

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