hp_sdc.c 28 KB

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