hp_sdc.c 28 KB

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