hp_sdc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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 <linux/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. hp_sdc.rtime = ktime_get();
  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. ktime_t now = ktime_get();
  262. if (ktime_after(now, ktime_add_us(hp_sdc.rtime,
  263. HP_SDC_MAX_REG_DELAY))) {
  264. hp_sdc_transaction *curr;
  265. uint8_t tmp;
  266. curr = hp_sdc.tq[hp_sdc.rcurr];
  267. /* If this turns out to be a normal failure mode
  268. * we'll need to figure out a way to communicate
  269. * it back to the application. and be less verbose.
  270. */
  271. printk(KERN_WARNING PREFIX "read timeout (%lldus)!\n",
  272. ktime_us_delta(now, hp_sdc.rtime));
  273. curr->idx += hp_sdc.rqty;
  274. hp_sdc.rqty = 0;
  275. tmp = curr->seq[curr->actidx];
  276. curr->seq[curr->actidx] |= HP_SDC_ACT_DEAD;
  277. if (tmp & HP_SDC_ACT_SEMAPHORE)
  278. if (curr->act.semaphore)
  279. up(curr->act.semaphore);
  280. if (tmp & HP_SDC_ACT_CALLBACK) {
  281. /* Note this means that irqhooks may be called
  282. * in tasklet/bh context.
  283. */
  284. if (curr->act.irqhook)
  285. curr->act.irqhook(0, NULL, 0, 0);
  286. }
  287. curr->actidx = curr->idx;
  288. curr->idx++;
  289. hp_sdc.rcurr = -1;
  290. }
  291. }
  292. write_unlock_irq(&hp_sdc.rtq_lock);
  293. hp_sdc_put();
  294. }
  295. unsigned long hp_sdc_put(void)
  296. {
  297. hp_sdc_transaction *curr;
  298. uint8_t act;
  299. int idx, curridx;
  300. int limit = 0;
  301. write_lock(&hp_sdc.lock);
  302. /* If i8042 buffers are full, we cannot do anything that
  303. requires output, so we skip to the administrativa. */
  304. if (hp_sdc.ibf) {
  305. hp_sdc_status_in8();
  306. if (hp_sdc.ibf)
  307. goto finish;
  308. }
  309. anew:
  310. /* See if we are in the middle of a sequence. */
  311. if (hp_sdc.wcurr < 0)
  312. hp_sdc.wcurr = 0;
  313. read_lock_irq(&hp_sdc.rtq_lock);
  314. if (hp_sdc.rcurr == hp_sdc.wcurr)
  315. hp_sdc.wcurr++;
  316. read_unlock_irq(&hp_sdc.rtq_lock);
  317. if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
  318. hp_sdc.wcurr = 0;
  319. curridx = hp_sdc.wcurr;
  320. if (hp_sdc.tq[curridx] != NULL)
  321. goto start;
  322. while (++curridx != hp_sdc.wcurr) {
  323. if (curridx >= HP_SDC_QUEUE_LEN) {
  324. curridx = -1; /* Wrap to top */
  325. continue;
  326. }
  327. read_lock_irq(&hp_sdc.rtq_lock);
  328. if (hp_sdc.rcurr == curridx) {
  329. read_unlock_irq(&hp_sdc.rtq_lock);
  330. continue;
  331. }
  332. read_unlock_irq(&hp_sdc.rtq_lock);
  333. if (hp_sdc.tq[curridx] != NULL)
  334. break; /* Found one. */
  335. }
  336. if (curridx == hp_sdc.wcurr) { /* There's nothing queued to do. */
  337. curridx = -1;
  338. }
  339. hp_sdc.wcurr = curridx;
  340. start:
  341. /* Check to see if the interrupt mask needs to be set. */
  342. if (hp_sdc.set_im) {
  343. hp_sdc_status_out8(hp_sdc.im | HP_SDC_CMD_SET_IM);
  344. hp_sdc.set_im = 0;
  345. goto finish;
  346. }
  347. if (hp_sdc.wcurr == -1)
  348. goto done;
  349. curr = hp_sdc.tq[curridx];
  350. idx = curr->actidx;
  351. if (curr->actidx >= curr->endidx) {
  352. hp_sdc.tq[curridx] = NULL;
  353. /* Interleave outbound data between the transactions. */
  354. hp_sdc.wcurr++;
  355. if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
  356. hp_sdc.wcurr = 0;
  357. goto finish;
  358. }
  359. act = curr->seq[idx];
  360. idx++;
  361. if (curr->idx >= curr->endidx) {
  362. if (act & HP_SDC_ACT_DEALLOC)
  363. kfree(curr);
  364. hp_sdc.tq[curridx] = NULL;
  365. /* Interleave outbound data between the transactions. */
  366. hp_sdc.wcurr++;
  367. if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
  368. hp_sdc.wcurr = 0;
  369. goto finish;
  370. }
  371. while (act & HP_SDC_ACT_PRECMD) {
  372. if (curr->idx != idx) {
  373. idx++;
  374. act &= ~HP_SDC_ACT_PRECMD;
  375. break;
  376. }
  377. hp_sdc_status_out8(curr->seq[idx]);
  378. curr->idx++;
  379. /* act finished? */
  380. if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_PRECMD)
  381. goto actdone;
  382. /* skip quantity field if data-out sequence follows. */
  383. if (act & HP_SDC_ACT_DATAOUT)
  384. curr->idx++;
  385. goto finish;
  386. }
  387. if (act & HP_SDC_ACT_DATAOUT) {
  388. int qty;
  389. qty = curr->seq[idx];
  390. idx++;
  391. if (curr->idx - idx < qty) {
  392. hp_sdc_data_out8(curr->seq[curr->idx]);
  393. curr->idx++;
  394. /* act finished? */
  395. if (curr->idx - idx >= qty &&
  396. (act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAOUT)
  397. goto actdone;
  398. goto finish;
  399. }
  400. idx += qty;
  401. act &= ~HP_SDC_ACT_DATAOUT;
  402. } else
  403. while (act & HP_SDC_ACT_DATAREG) {
  404. int mask;
  405. uint8_t w7[4];
  406. mask = curr->seq[idx];
  407. if (idx != curr->idx) {
  408. idx++;
  409. idx += !!(mask & 1);
  410. idx += !!(mask & 2);
  411. idx += !!(mask & 4);
  412. idx += !!(mask & 8);
  413. act &= ~HP_SDC_ACT_DATAREG;
  414. break;
  415. }
  416. w7[0] = (mask & 1) ? curr->seq[++idx] : hp_sdc.r7[0];
  417. w7[1] = (mask & 2) ? curr->seq[++idx] : hp_sdc.r7[1];
  418. w7[2] = (mask & 4) ? curr->seq[++idx] : hp_sdc.r7[2];
  419. w7[3] = (mask & 8) ? curr->seq[++idx] : hp_sdc.r7[3];
  420. if (hp_sdc.wi > 0x73 || hp_sdc.wi < 0x70 ||
  421. w7[hp_sdc.wi - 0x70] == hp_sdc.r7[hp_sdc.wi - 0x70]) {
  422. int i = 0;
  423. /* Need to point the write index register */
  424. while (i < 4 && w7[i] == hp_sdc.r7[i])
  425. i++;
  426. if (i < 4) {
  427. hp_sdc_status_out8(HP_SDC_CMD_SET_D0 + i);
  428. hp_sdc.wi = 0x70 + i;
  429. goto finish;
  430. }
  431. idx++;
  432. if ((act & HP_SDC_ACT_DURING) == HP_SDC_ACT_DATAREG)
  433. goto actdone;
  434. curr->idx = idx;
  435. act &= ~HP_SDC_ACT_DATAREG;
  436. break;
  437. }
  438. hp_sdc_data_out8(w7[hp_sdc.wi - 0x70]);
  439. hp_sdc.r7[hp_sdc.wi - 0x70] = w7[hp_sdc.wi - 0x70];
  440. hp_sdc.wi++; /* write index register autoincrements */
  441. {
  442. int i = 0;
  443. while ((i < 4) && w7[i] == hp_sdc.r7[i])
  444. i++;
  445. if (i >= 4) {
  446. curr->idx = idx + 1;
  447. if ((act & HP_SDC_ACT_DURING) ==
  448. HP_SDC_ACT_DATAREG)
  449. goto actdone;
  450. }
  451. }
  452. goto finish;
  453. }
  454. /* We don't go any further in the command if there is a pending read,
  455. because we don't want interleaved results. */
  456. read_lock_irq(&hp_sdc.rtq_lock);
  457. if (hp_sdc.rcurr >= 0) {
  458. read_unlock_irq(&hp_sdc.rtq_lock);
  459. goto finish;
  460. }
  461. read_unlock_irq(&hp_sdc.rtq_lock);
  462. if (act & HP_SDC_ACT_POSTCMD) {
  463. uint8_t postcmd;
  464. /* curr->idx should == idx at this point. */
  465. postcmd = curr->seq[idx];
  466. curr->idx++;
  467. if (act & HP_SDC_ACT_DATAIN) {
  468. /* Start a new read */
  469. hp_sdc.rqty = curr->seq[curr->idx];
  470. hp_sdc.rtime = ktime_get();
  471. curr->idx++;
  472. /* Still need to lock here in case of spurious irq. */
  473. write_lock_irq(&hp_sdc.rtq_lock);
  474. hp_sdc.rcurr = curridx;
  475. write_unlock_irq(&hp_sdc.rtq_lock);
  476. hp_sdc_status_out8(postcmd);
  477. goto finish;
  478. }
  479. hp_sdc_status_out8(postcmd);
  480. goto actdone;
  481. }
  482. actdone:
  483. if (act & HP_SDC_ACT_SEMAPHORE)
  484. up(curr->act.semaphore);
  485. else if (act & HP_SDC_ACT_CALLBACK)
  486. curr->act.irqhook(0,NULL,0,0);
  487. if (curr->idx >= curr->endidx) { /* This transaction is over. */
  488. if (act & HP_SDC_ACT_DEALLOC)
  489. kfree(curr);
  490. hp_sdc.tq[curridx] = NULL;
  491. } else {
  492. curr->actidx = idx + 1;
  493. curr->idx = idx + 2;
  494. }
  495. /* Interleave outbound data between the transactions. */
  496. hp_sdc.wcurr++;
  497. if (hp_sdc.wcurr >= HP_SDC_QUEUE_LEN)
  498. hp_sdc.wcurr = 0;
  499. finish:
  500. /* If by some quirk IBF has cleared and our ISR has run to
  501. see that that has happened, do it all again. */
  502. if (!hp_sdc.ibf && limit++ < 20)
  503. goto anew;
  504. done:
  505. if (hp_sdc.wcurr >= 0)
  506. tasklet_schedule(&hp_sdc.task);
  507. write_unlock(&hp_sdc.lock);
  508. return 0;
  509. }
  510. /******* Functions called in either user or kernel context ****/
  511. int __hp_sdc_enqueue_transaction(hp_sdc_transaction *this)
  512. {
  513. int i;
  514. if (this == NULL) {
  515. BUG();
  516. return -EINVAL;
  517. }
  518. /* Can't have same transaction on queue twice */
  519. for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
  520. if (hp_sdc.tq[i] == this)
  521. goto fail;
  522. this->actidx = 0;
  523. this->idx = 1;
  524. /* Search for empty slot */
  525. for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
  526. if (hp_sdc.tq[i] == NULL) {
  527. hp_sdc.tq[i] = this;
  528. tasklet_schedule(&hp_sdc.task);
  529. return 0;
  530. }
  531. printk(KERN_WARNING PREFIX "No free slot to add transaction.\n");
  532. return -EBUSY;
  533. fail:
  534. printk(KERN_WARNING PREFIX "Transaction add failed: transaction already queued?\n");
  535. return -EINVAL;
  536. }
  537. int hp_sdc_enqueue_transaction(hp_sdc_transaction *this) {
  538. unsigned long flags;
  539. int ret;
  540. write_lock_irqsave(&hp_sdc.lock, flags);
  541. ret = __hp_sdc_enqueue_transaction(this);
  542. write_unlock_irqrestore(&hp_sdc.lock,flags);
  543. return ret;
  544. }
  545. int hp_sdc_dequeue_transaction(hp_sdc_transaction *this)
  546. {
  547. unsigned long flags;
  548. int i;
  549. write_lock_irqsave(&hp_sdc.lock, flags);
  550. /* TODO: don't remove it if it's not done. */
  551. for (i = 0; i < HP_SDC_QUEUE_LEN; i++)
  552. if (hp_sdc.tq[i] == this)
  553. hp_sdc.tq[i] = NULL;
  554. write_unlock_irqrestore(&hp_sdc.lock, flags);
  555. return 0;
  556. }
  557. /********************** User context functions **************************/
  558. int hp_sdc_request_timer_irq(hp_sdc_irqhook *callback)
  559. {
  560. if (callback == NULL || hp_sdc.dev == NULL)
  561. return -EINVAL;
  562. write_lock_irq(&hp_sdc.hook_lock);
  563. if (hp_sdc.timer != NULL) {
  564. write_unlock_irq(&hp_sdc.hook_lock);
  565. return -EBUSY;
  566. }
  567. hp_sdc.timer = callback;
  568. /* Enable interrupts from the timers */
  569. hp_sdc.im &= ~HP_SDC_IM_FH;
  570. hp_sdc.im &= ~HP_SDC_IM_PT;
  571. hp_sdc.im &= ~HP_SDC_IM_TIMERS;
  572. hp_sdc.set_im = 1;
  573. write_unlock_irq(&hp_sdc.hook_lock);
  574. tasklet_schedule(&hp_sdc.task);
  575. return 0;
  576. }
  577. int hp_sdc_request_hil_irq(hp_sdc_irqhook *callback)
  578. {
  579. if (callback == NULL || hp_sdc.dev == NULL)
  580. return -EINVAL;
  581. write_lock_irq(&hp_sdc.hook_lock);
  582. if (hp_sdc.hil != NULL) {
  583. write_unlock_irq(&hp_sdc.hook_lock);
  584. return -EBUSY;
  585. }
  586. hp_sdc.hil = callback;
  587. hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
  588. hp_sdc.set_im = 1;
  589. write_unlock_irq(&hp_sdc.hook_lock);
  590. tasklet_schedule(&hp_sdc.task);
  591. return 0;
  592. }
  593. int hp_sdc_request_cooked_irq(hp_sdc_irqhook *callback)
  594. {
  595. if (callback == NULL || hp_sdc.dev == NULL)
  596. return -EINVAL;
  597. write_lock_irq(&hp_sdc.hook_lock);
  598. if (hp_sdc.cooked != NULL) {
  599. write_unlock_irq(&hp_sdc.hook_lock);
  600. return -EBUSY;
  601. }
  602. /* Enable interrupts from the HIL MLC */
  603. hp_sdc.cooked = callback;
  604. hp_sdc.im &= ~(HP_SDC_IM_HIL | HP_SDC_IM_RESET);
  605. hp_sdc.set_im = 1;
  606. write_unlock_irq(&hp_sdc.hook_lock);
  607. tasklet_schedule(&hp_sdc.task);
  608. return 0;
  609. }
  610. int hp_sdc_release_timer_irq(hp_sdc_irqhook *callback)
  611. {
  612. write_lock_irq(&hp_sdc.hook_lock);
  613. if ((callback != hp_sdc.timer) ||
  614. (hp_sdc.timer == NULL)) {
  615. write_unlock_irq(&hp_sdc.hook_lock);
  616. return -EINVAL;
  617. }
  618. /* Disable interrupts from the timers */
  619. hp_sdc.timer = NULL;
  620. hp_sdc.im |= HP_SDC_IM_TIMERS;
  621. hp_sdc.im |= HP_SDC_IM_FH;
  622. hp_sdc.im |= HP_SDC_IM_PT;
  623. hp_sdc.set_im = 1;
  624. write_unlock_irq(&hp_sdc.hook_lock);
  625. tasklet_schedule(&hp_sdc.task);
  626. return 0;
  627. }
  628. int hp_sdc_release_hil_irq(hp_sdc_irqhook *callback)
  629. {
  630. write_lock_irq(&hp_sdc.hook_lock);
  631. if ((callback != hp_sdc.hil) ||
  632. (hp_sdc.hil == NULL)) {
  633. write_unlock_irq(&hp_sdc.hook_lock);
  634. return -EINVAL;
  635. }
  636. hp_sdc.hil = NULL;
  637. /* Disable interrupts from HIL only if there is no cooked driver. */
  638. if(hp_sdc.cooked == NULL) {
  639. hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
  640. hp_sdc.set_im = 1;
  641. }
  642. write_unlock_irq(&hp_sdc.hook_lock);
  643. tasklet_schedule(&hp_sdc.task);
  644. return 0;
  645. }
  646. int hp_sdc_release_cooked_irq(hp_sdc_irqhook *callback)
  647. {
  648. write_lock_irq(&hp_sdc.hook_lock);
  649. if ((callback != hp_sdc.cooked) ||
  650. (hp_sdc.cooked == NULL)) {
  651. write_unlock_irq(&hp_sdc.hook_lock);
  652. return -EINVAL;
  653. }
  654. hp_sdc.cooked = NULL;
  655. /* Disable interrupts from HIL only if there is no raw HIL driver. */
  656. if(hp_sdc.hil == NULL) {
  657. hp_sdc.im |= (HP_SDC_IM_HIL | HP_SDC_IM_RESET);
  658. hp_sdc.set_im = 1;
  659. }
  660. write_unlock_irq(&hp_sdc.hook_lock);
  661. tasklet_schedule(&hp_sdc.task);
  662. return 0;
  663. }
  664. /************************* Keepalive timer task *********************/
  665. static void hp_sdc_kicker(struct timer_list *unused)
  666. {
  667. tasklet_schedule(&hp_sdc.task);
  668. /* Re-insert the periodic task. */
  669. mod_timer(&hp_sdc.kicker, jiffies + HZ);
  670. }
  671. /************************** Module Initialization ***************************/
  672. #if defined(__hppa__)
  673. static const struct parisc_device_id hp_sdc_tbl[] __initconst = {
  674. {
  675. .hw_type = HPHW_FIO,
  676. .hversion_rev = HVERSION_REV_ANY_ID,
  677. .hversion = HVERSION_ANY_ID,
  678. .sversion = 0x73,
  679. },
  680. { 0, }
  681. };
  682. MODULE_DEVICE_TABLE(parisc, hp_sdc_tbl);
  683. static int __init hp_sdc_init_hppa(struct parisc_device *d);
  684. static struct delayed_work moduleloader_work;
  685. static struct parisc_driver hp_sdc_driver __refdata = {
  686. .name = "hp_sdc",
  687. .id_table = hp_sdc_tbl,
  688. .probe = hp_sdc_init_hppa,
  689. };
  690. #endif /* __hppa__ */
  691. static int __init hp_sdc_init(void)
  692. {
  693. char *errstr;
  694. hp_sdc_transaction t_sync;
  695. uint8_t ts_sync[6];
  696. struct semaphore s_sync;
  697. rwlock_init(&hp_sdc.lock);
  698. rwlock_init(&hp_sdc.ibf_lock);
  699. rwlock_init(&hp_sdc.rtq_lock);
  700. rwlock_init(&hp_sdc.hook_lock);
  701. hp_sdc.timer = NULL;
  702. hp_sdc.hil = NULL;
  703. hp_sdc.pup = NULL;
  704. hp_sdc.cooked = NULL;
  705. hp_sdc.im = HP_SDC_IM_MASK; /* Mask maskable irqs */
  706. hp_sdc.set_im = 1;
  707. hp_sdc.wi = 0xff;
  708. hp_sdc.r7[0] = 0xff;
  709. hp_sdc.r7[1] = 0xff;
  710. hp_sdc.r7[2] = 0xff;
  711. hp_sdc.r7[3] = 0xff;
  712. hp_sdc.ibf = 1;
  713. memset(&hp_sdc.tq, 0, sizeof(hp_sdc.tq));
  714. hp_sdc.wcurr = -1;
  715. hp_sdc.rcurr = -1;
  716. hp_sdc.rqty = 0;
  717. hp_sdc.dev_err = -ENODEV;
  718. errstr = "IO not found for";
  719. if (!hp_sdc.base_io)
  720. goto err0;
  721. errstr = "IRQ not found for";
  722. if (!hp_sdc.irq)
  723. goto err0;
  724. hp_sdc.dev_err = -EBUSY;
  725. #if defined(__hppa__)
  726. errstr = "IO not available for";
  727. if (request_region(hp_sdc.data_io, 2, hp_sdc_driver.name))
  728. goto err0;
  729. #endif
  730. errstr = "IRQ not available for";
  731. if (request_irq(hp_sdc.irq, &hp_sdc_isr, IRQF_SHARED,
  732. "HP SDC", &hp_sdc))
  733. goto err1;
  734. errstr = "NMI not available for";
  735. if (request_irq(hp_sdc.nmi, &hp_sdc_nmisr, IRQF_SHARED,
  736. "HP SDC NMI", &hp_sdc))
  737. goto err2;
  738. pr_info(PREFIX "HP SDC at 0x%08lx, IRQ %d (NMI IRQ %d)\n",
  739. hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
  740. hp_sdc_status_in8();
  741. hp_sdc_data_in8();
  742. tasklet_init(&hp_sdc.task, hp_sdc_tasklet, 0);
  743. /* Sync the output buffer registers, thus scheduling hp_sdc_tasklet. */
  744. t_sync.actidx = 0;
  745. t_sync.idx = 1;
  746. t_sync.endidx = 6;
  747. t_sync.seq = ts_sync;
  748. ts_sync[0] = HP_SDC_ACT_DATAREG | HP_SDC_ACT_SEMAPHORE;
  749. ts_sync[1] = 0x0f;
  750. ts_sync[2] = ts_sync[3] = ts_sync[4] = ts_sync[5] = 0;
  751. t_sync.act.semaphore = &s_sync;
  752. sema_init(&s_sync, 0);
  753. hp_sdc_enqueue_transaction(&t_sync);
  754. down(&s_sync); /* Wait for t_sync to complete */
  755. /* Create the keepalive task */
  756. timer_setup(&hp_sdc.kicker, hp_sdc_kicker, 0);
  757. hp_sdc.kicker.expires = jiffies + HZ;
  758. add_timer(&hp_sdc.kicker);
  759. hp_sdc.dev_err = 0;
  760. return 0;
  761. err2:
  762. free_irq(hp_sdc.irq, &hp_sdc);
  763. err1:
  764. release_region(hp_sdc.data_io, 2);
  765. err0:
  766. printk(KERN_WARNING PREFIX ": %s SDC IO=0x%p IRQ=0x%x NMI=0x%x\n",
  767. errstr, (void *)hp_sdc.base_io, hp_sdc.irq, hp_sdc.nmi);
  768. hp_sdc.dev = NULL;
  769. return hp_sdc.dev_err;
  770. }
  771. #if defined(__hppa__)
  772. static void request_module_delayed(struct work_struct *work)
  773. {
  774. request_module("hp_sdc_mlc");
  775. }
  776. static int __init hp_sdc_init_hppa(struct parisc_device *d)
  777. {
  778. int ret;
  779. if (!d)
  780. return 1;
  781. if (hp_sdc.dev != NULL)
  782. return 1; /* We only expect one SDC */
  783. hp_sdc.dev = d;
  784. hp_sdc.irq = d->irq;
  785. hp_sdc.nmi = d->aux_irq;
  786. hp_sdc.base_io = d->hpa.start;
  787. hp_sdc.data_io = d->hpa.start + 0x800;
  788. hp_sdc.status_io = d->hpa.start + 0x801;
  789. INIT_DELAYED_WORK(&moduleloader_work, request_module_delayed);
  790. ret = hp_sdc_init();
  791. /* after successful initialization give SDC some time to settle
  792. * and then load the hp_sdc_mlc upper layer driver */
  793. if (!ret)
  794. schedule_delayed_work(&moduleloader_work,
  795. msecs_to_jiffies(2000));
  796. return ret;
  797. }
  798. #endif /* __hppa__ */
  799. static void hp_sdc_exit(void)
  800. {
  801. /* do nothing if we don't have a SDC */
  802. if (!hp_sdc.dev)
  803. return;
  804. write_lock_irq(&hp_sdc.lock);
  805. /* Turn off all maskable "sub-function" irq's. */
  806. hp_sdc_spin_ibf();
  807. sdc_writeb(HP_SDC_CMD_SET_IM | HP_SDC_IM_MASK, hp_sdc.status_io);
  808. /* Wait until we know this has been processed by the i8042 */
  809. hp_sdc_spin_ibf();
  810. free_irq(hp_sdc.nmi, &hp_sdc);
  811. free_irq(hp_sdc.irq, &hp_sdc);
  812. write_unlock_irq(&hp_sdc.lock);
  813. del_timer_sync(&hp_sdc.kicker);
  814. tasklet_kill(&hp_sdc.task);
  815. #if defined(__hppa__)
  816. cancel_delayed_work_sync(&moduleloader_work);
  817. if (unregister_parisc_driver(&hp_sdc_driver))
  818. printk(KERN_WARNING PREFIX "Error unregistering HP SDC");
  819. #endif
  820. }
  821. static int __init hp_sdc_register(void)
  822. {
  823. hp_sdc_transaction tq_init;
  824. uint8_t tq_init_seq[5];
  825. struct semaphore tq_init_sem;
  826. #if defined(__mc68000__)
  827. unsigned char i;
  828. #endif
  829. if (hp_sdc_disabled) {
  830. printk(KERN_WARNING PREFIX "HP SDC driver disabled by no_hpsdc=1.\n");
  831. return -ENODEV;
  832. }
  833. hp_sdc.dev = NULL;
  834. hp_sdc.dev_err = 0;
  835. #if defined(__hppa__)
  836. if (register_parisc_driver(&hp_sdc_driver)) {
  837. printk(KERN_WARNING PREFIX "Error registering SDC with system bus tree.\n");
  838. return -ENODEV;
  839. }
  840. #elif defined(__mc68000__)
  841. if (!MACH_IS_HP300)
  842. return -ENODEV;
  843. hp_sdc.irq = 1;
  844. hp_sdc.nmi = 7;
  845. hp_sdc.base_io = (unsigned long) 0xf0428000;
  846. hp_sdc.data_io = (unsigned long) hp_sdc.base_io + 1;
  847. hp_sdc.status_io = (unsigned long) hp_sdc.base_io + 3;
  848. if (!probe_kernel_read(&i, (unsigned char *)hp_sdc.data_io, 1))
  849. hp_sdc.dev = (void *)1;
  850. hp_sdc.dev_err = hp_sdc_init();
  851. #endif
  852. if (hp_sdc.dev == NULL) {
  853. printk(KERN_WARNING PREFIX "No SDC found.\n");
  854. return hp_sdc.dev_err;
  855. }
  856. sema_init(&tq_init_sem, 0);
  857. tq_init.actidx = 0;
  858. tq_init.idx = 1;
  859. tq_init.endidx = 5;
  860. tq_init.seq = tq_init_seq;
  861. tq_init.act.semaphore = &tq_init_sem;
  862. tq_init_seq[0] =
  863. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN | HP_SDC_ACT_SEMAPHORE;
  864. tq_init_seq[1] = HP_SDC_CMD_READ_KCC;
  865. tq_init_seq[2] = 1;
  866. tq_init_seq[3] = 0;
  867. tq_init_seq[4] = 0;
  868. hp_sdc_enqueue_transaction(&tq_init);
  869. down(&tq_init_sem);
  870. up(&tq_init_sem);
  871. if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
  872. printk(KERN_WARNING PREFIX "Error reading config byte.\n");
  873. hp_sdc_exit();
  874. return -ENODEV;
  875. }
  876. hp_sdc.r11 = tq_init_seq[4];
  877. if (hp_sdc.r11 & HP_SDC_CFG_NEW) {
  878. const char *str;
  879. printk(KERN_INFO PREFIX "New style SDC\n");
  880. tq_init_seq[1] = HP_SDC_CMD_READ_XTD;
  881. tq_init.actidx = 0;
  882. tq_init.idx = 1;
  883. down(&tq_init_sem);
  884. hp_sdc_enqueue_transaction(&tq_init);
  885. down(&tq_init_sem);
  886. up(&tq_init_sem);
  887. if ((tq_init_seq[0] & HP_SDC_ACT_DEAD) == HP_SDC_ACT_DEAD) {
  888. printk(KERN_WARNING PREFIX "Error reading extended config byte.\n");
  889. return -ENODEV;
  890. }
  891. hp_sdc.r7e = tq_init_seq[4];
  892. HP_SDC_XTD_REV_STRINGS(hp_sdc.r7e & HP_SDC_XTD_REV, str)
  893. printk(KERN_INFO PREFIX "Revision: %s\n", str);
  894. if (hp_sdc.r7e & HP_SDC_XTD_BEEPER)
  895. printk(KERN_INFO PREFIX "TI SN76494 beeper present\n");
  896. if (hp_sdc.r7e & HP_SDC_XTD_BBRTC)
  897. printk(KERN_INFO PREFIX "OKI MSM-58321 BBRTC present\n");
  898. printk(KERN_INFO PREFIX "Spunking the self test register to force PUP "
  899. "on next firmware reset.\n");
  900. tq_init_seq[0] = HP_SDC_ACT_PRECMD |
  901. HP_SDC_ACT_DATAOUT | HP_SDC_ACT_SEMAPHORE;
  902. tq_init_seq[1] = HP_SDC_CMD_SET_STR;
  903. tq_init_seq[2] = 1;
  904. tq_init_seq[3] = 0;
  905. tq_init.actidx = 0;
  906. tq_init.idx = 1;
  907. tq_init.endidx = 4;
  908. down(&tq_init_sem);
  909. hp_sdc_enqueue_transaction(&tq_init);
  910. down(&tq_init_sem);
  911. up(&tq_init_sem);
  912. } else
  913. printk(KERN_INFO PREFIX "Old style SDC (1820-%s).\n",
  914. (hp_sdc.r11 & HP_SDC_CFG_REV) ? "3300" : "2564/3087");
  915. return 0;
  916. }
  917. module_init(hp_sdc_register);
  918. module_exit(hp_sdc_exit);
  919. /* Timing notes: These measurements taken on my 64MHz 7100-LC (715/64)
  920. * cycles cycles-adj time
  921. * between two consecutive mfctl(16)'s: 4 n/a 63ns
  922. * hp_sdc_spin_ibf when idle: 119 115 1.7us
  923. * gsc_writeb status register: 83 79 1.2us
  924. * IBF to clear after sending SET_IM: 6204 6006 93us
  925. * IBF to clear after sending LOAD_RT: 4467 4352 68us
  926. * IBF to clear after sending two LOAD_RTs: 18974 18859 295us
  927. * READ_T1, read status/data, IRQ, call handler: 35564 n/a 556us
  928. * cmd to ~IBF READ_T1 2nd time right after: 5158403 n/a 81ms
  929. * between IRQ received and ~IBF for above: 2578877 n/a 40ms
  930. *
  931. * Performance stats after a run of this module configuring HIL and
  932. * receiving a few mouse events:
  933. *
  934. * status in8 282508 cycles 7128 calls
  935. * status out8 8404 cycles 341 calls
  936. * data out8 1734 cycles 78 calls
  937. * isr 174324 cycles 617 calls (includes take)
  938. * take 1241 cycles 2 calls
  939. * put 1411504 cycles 6937 calls
  940. * task 1655209 cycles 6937 calls (includes put)
  941. *
  942. */