pcxhr_core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /*
  2. * Driver for Digigram pcxhr compatible soundcards
  3. *
  4. * low level interface with interrupt and message handling implementation
  5. *
  6. * Copyright (c) 2004 by Digigram <alsa@digigram.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/firmware.h>
  24. #include <linux/interrupt.h>
  25. #include <asm/io.h>
  26. #include <sound/core.h>
  27. #include "pcxhr.h"
  28. #include "pcxhr_mixer.h"
  29. #include "pcxhr_hwdep.h"
  30. #include "pcxhr_core.h"
  31. /* registers used on the PLX (port 1) */
  32. #define PCXHR_PLX_OFFSET_MIN 0x40
  33. #define PCXHR_PLX_MBOX0 0x40
  34. #define PCXHR_PLX_MBOX1 0x44
  35. #define PCXHR_PLX_MBOX2 0x48
  36. #define PCXHR_PLX_MBOX3 0x4C
  37. #define PCXHR_PLX_MBOX4 0x50
  38. #define PCXHR_PLX_MBOX5 0x54
  39. #define PCXHR_PLX_MBOX6 0x58
  40. #define PCXHR_PLX_MBOX7 0x5C
  41. #define PCXHR_PLX_L2PCIDB 0x64
  42. #define PCXHR_PLX_IRQCS 0x68
  43. #define PCXHR_PLX_CHIPSC 0x6C
  44. /* registers used on the DSP (port 2) */
  45. #define PCXHR_DSP_ICR 0x00
  46. #define PCXHR_DSP_CVR 0x04
  47. #define PCXHR_DSP_ISR 0x08
  48. #define PCXHR_DSP_IVR 0x0C
  49. #define PCXHR_DSP_RXH 0x14
  50. #define PCXHR_DSP_TXH 0x14
  51. #define PCXHR_DSP_RXM 0x18
  52. #define PCXHR_DSP_TXM 0x18
  53. #define PCXHR_DSP_RXL 0x1C
  54. #define PCXHR_DSP_TXL 0x1C
  55. #define PCXHR_DSP_RESET 0x20
  56. #define PCXHR_DSP_OFFSET_MAX 0x20
  57. /* access to the card */
  58. #define PCXHR_PLX 1
  59. #define PCXHR_DSP 2
  60. #if (PCXHR_DSP_OFFSET_MAX > PCXHR_PLX_OFFSET_MIN)
  61. #undef PCXHR_REG_TO_PORT(x)
  62. #else
  63. #define PCXHR_REG_TO_PORT(x) ((x)>PCXHR_DSP_OFFSET_MAX ? PCXHR_PLX : PCXHR_DSP)
  64. #endif
  65. #define PCXHR_INPB(mgr,x) inb((mgr)->port[PCXHR_REG_TO_PORT(x)] + (x))
  66. #define PCXHR_INPL(mgr,x) inl((mgr)->port[PCXHR_REG_TO_PORT(x)] + (x))
  67. #define PCXHR_OUTPB(mgr,x,data) outb((data), (mgr)->port[PCXHR_REG_TO_PORT(x)] + (x))
  68. #define PCXHR_OUTPL(mgr,x,data) outl((data), (mgr)->port[PCXHR_REG_TO_PORT(x)] + (x))
  69. /* attention : access the PCXHR_DSP_* registers with inb and outb only ! */
  70. /* params used with PCXHR_PLX_MBOX0 */
  71. #define PCXHR_MBOX0_HF5 (1 << 0)
  72. #define PCXHR_MBOX0_HF4 (1 << 1)
  73. #define PCXHR_MBOX0_BOOT_HERE (1 << 23)
  74. /* params used with PCXHR_PLX_IRQCS */
  75. #define PCXHR_IRQCS_ENABLE_PCIIRQ (1 << 8)
  76. #define PCXHR_IRQCS_ENABLE_PCIDB (1 << 9)
  77. #define PCXHR_IRQCS_ACTIVE_PCIDB (1 << 13)
  78. /* params used with PCXHR_PLX_CHIPSC */
  79. #define PCXHR_CHIPSC_INIT_VALUE 0x100D767E
  80. #define PCXHR_CHIPSC_RESET_XILINX (1 << 16)
  81. #define PCXHR_CHIPSC_GPI_USERI (1 << 17)
  82. #define PCXHR_CHIPSC_DATA_CLK (1 << 24)
  83. #define PCXHR_CHIPSC_DATA_IN (1 << 26)
  84. /* params used with PCXHR_DSP_ICR */
  85. #define PCXHR_ICR_HI08_RREQ 0x01
  86. #define PCXHR_ICR_HI08_TREQ 0x02
  87. #define PCXHR_ICR_HI08_HDRQ 0x04
  88. #define PCXHR_ICR_HI08_HF0 0x08
  89. #define PCXHR_ICR_HI08_HF1 0x10
  90. #define PCXHR_ICR_HI08_HLEND 0x20
  91. #define PCXHR_ICR_HI08_INIT 0x80
  92. /* params used with PCXHR_DSP_CVR */
  93. #define PCXHR_CVR_HI08_HC 0x80
  94. /* params used with PCXHR_DSP_ISR */
  95. #define PCXHR_ISR_HI08_RXDF 0x01
  96. #define PCXHR_ISR_HI08_TXDE 0x02
  97. #define PCXHR_ISR_HI08_TRDY 0x04
  98. #define PCXHR_ISR_HI08_ERR 0x08
  99. #define PCXHR_ISR_HI08_CHK 0x10
  100. #define PCXHR_ISR_HI08_HREQ 0x80
  101. /* constants used for delay in msec */
  102. #define PCXHR_WAIT_DEFAULT 2
  103. #define PCXHR_WAIT_IT 25
  104. #define PCXHR_WAIT_IT_EXTRA 65
  105. /*
  106. * pcxhr_check_reg_bit - wait for the specified bit is set/reset on a register
  107. * @reg: register to check
  108. * @mask: bit mask
  109. * @bit: resultant bit to be checked
  110. * @time: time-out of loop in msec
  111. *
  112. * returns zero if a bit matches, or a negative error code.
  113. */
  114. static int pcxhr_check_reg_bit(struct pcxhr_mgr *mgr, unsigned int reg,
  115. unsigned char mask, unsigned char bit, int time,
  116. unsigned char* read)
  117. {
  118. int i = 0;
  119. unsigned long end_time = jiffies + (time * HZ + 999) / 1000;
  120. do {
  121. *read = PCXHR_INPB(mgr, reg);
  122. if ((*read & mask) == bit) {
  123. if (i > 100)
  124. snd_printdd("ATTENTION! check_reg(%x) loopcount=%d\n",
  125. reg, i);
  126. return 0;
  127. }
  128. i++;
  129. } while (time_after_eq(end_time, jiffies));
  130. snd_printk(KERN_ERR "pcxhr_check_reg_bit: timeout, reg=%x, mask=0x%x, val=0x%x\n",
  131. reg, mask, *read);
  132. return -EIO;
  133. }
  134. /* constants used with pcxhr_check_reg_bit() */
  135. #define PCXHR_TIMEOUT_DSP 200
  136. #define PCXHR_MASK_EXTRA_INFO 0x0000FE
  137. #define PCXHR_MASK_IT_HF0 0x000100
  138. #define PCXHR_MASK_IT_HF1 0x000200
  139. #define PCXHR_MASK_IT_NO_HF0_HF1 0x000400
  140. #define PCXHR_MASK_IT_MANAGE_HF5 0x000800
  141. #define PCXHR_MASK_IT_WAIT 0x010000
  142. #define PCXHR_MASK_IT_WAIT_EXTRA 0x020000
  143. #define PCXHR_IT_SEND_BYTE_XILINX (0x0000003C | PCXHR_MASK_IT_HF0)
  144. #define PCXHR_IT_TEST_XILINX (0x0000003C | PCXHR_MASK_IT_HF1 | \
  145. PCXHR_MASK_IT_MANAGE_HF5)
  146. #define PCXHR_IT_DOWNLOAD_BOOT (0x0000000C | PCXHR_MASK_IT_HF1 | \
  147. PCXHR_MASK_IT_MANAGE_HF5 | PCXHR_MASK_IT_WAIT)
  148. #define PCXHR_IT_RESET_BOARD_FUNC (0x0000000C | PCXHR_MASK_IT_HF0 | \
  149. PCXHR_MASK_IT_MANAGE_HF5 | PCXHR_MASK_IT_WAIT_EXTRA)
  150. #define PCXHR_IT_DOWNLOAD_DSP (0x0000000C | \
  151. PCXHR_MASK_IT_MANAGE_HF5 | PCXHR_MASK_IT_WAIT)
  152. #define PCXHR_IT_DEBUG (0x0000005A | PCXHR_MASK_IT_NO_HF0_HF1)
  153. #define PCXHR_IT_RESET_SEMAPHORE (0x0000005C | PCXHR_MASK_IT_NO_HF0_HF1)
  154. #define PCXHR_IT_MESSAGE (0x00000074 | PCXHR_MASK_IT_NO_HF0_HF1)
  155. #define PCXHR_IT_RESET_CHK (0x00000076 | PCXHR_MASK_IT_NO_HF0_HF1)
  156. #define PCXHR_IT_UPDATE_RBUFFER (0x00000078 | PCXHR_MASK_IT_NO_HF0_HF1)
  157. static int pcxhr_send_it_dsp(struct pcxhr_mgr *mgr, unsigned int itdsp, int atomic)
  158. {
  159. int err;
  160. unsigned char reg;
  161. if (itdsp & PCXHR_MASK_IT_MANAGE_HF5) {
  162. /* clear hf5 bit */
  163. PCXHR_OUTPL(mgr, PCXHR_PLX_MBOX0,
  164. PCXHR_INPL(mgr, PCXHR_PLX_MBOX0) & ~PCXHR_MBOX0_HF5);
  165. }
  166. if ((itdsp & PCXHR_MASK_IT_NO_HF0_HF1) == 0) {
  167. reg = PCXHR_ICR_HI08_RREQ | PCXHR_ICR_HI08_TREQ | PCXHR_ICR_HI08_HDRQ;
  168. if (itdsp & PCXHR_MASK_IT_HF0)
  169. reg |= PCXHR_ICR_HI08_HF0;
  170. if (itdsp & PCXHR_MASK_IT_HF1)
  171. reg |= PCXHR_ICR_HI08_HF1;
  172. PCXHR_OUTPB(mgr, PCXHR_DSP_ICR, reg);
  173. }
  174. reg = (unsigned char)(((itdsp & PCXHR_MASK_EXTRA_INFO) >> 1) | PCXHR_CVR_HI08_HC);
  175. PCXHR_OUTPB(mgr, PCXHR_DSP_CVR, reg);
  176. if (itdsp & PCXHR_MASK_IT_WAIT) {
  177. if (atomic)
  178. mdelay(PCXHR_WAIT_IT);
  179. else
  180. msleep(PCXHR_WAIT_IT);
  181. }
  182. if (itdsp & PCXHR_MASK_IT_WAIT_EXTRA) {
  183. if (atomic)
  184. mdelay(PCXHR_WAIT_IT_EXTRA);
  185. else
  186. msleep(PCXHR_WAIT_IT);
  187. }
  188. /* wait for CVR_HI08_HC == 0 */
  189. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_CVR, PCXHR_CVR_HI08_HC, 0,
  190. PCXHR_TIMEOUT_DSP, &reg);
  191. if (err) {
  192. snd_printk(KERN_ERR "pcxhr_send_it_dsp : TIMEOUT CVR\n");
  193. return err;
  194. }
  195. if (itdsp & PCXHR_MASK_IT_MANAGE_HF5) {
  196. /* wait for hf5 bit */
  197. err = pcxhr_check_reg_bit(mgr, PCXHR_PLX_MBOX0, PCXHR_MBOX0_HF5,
  198. PCXHR_MBOX0_HF5, PCXHR_TIMEOUT_DSP, &reg);
  199. if (err) {
  200. snd_printk(KERN_ERR "pcxhr_send_it_dsp : TIMEOUT HF5\n");
  201. return err;
  202. }
  203. }
  204. return 0; /* retry not handled here */
  205. }
  206. void pcxhr_reset_xilinx_com(struct pcxhr_mgr *mgr)
  207. {
  208. /* reset second xilinx */
  209. PCXHR_OUTPL(mgr, PCXHR_PLX_CHIPSC,
  210. PCXHR_CHIPSC_INIT_VALUE & ~PCXHR_CHIPSC_RESET_XILINX);
  211. }
  212. static void pcxhr_enable_irq(struct pcxhr_mgr *mgr, int enable)
  213. {
  214. unsigned int reg = PCXHR_INPL(mgr, PCXHR_PLX_IRQCS);
  215. /* enable/disable interrupts */
  216. if (enable)
  217. reg |= (PCXHR_IRQCS_ENABLE_PCIIRQ | PCXHR_IRQCS_ENABLE_PCIDB);
  218. else
  219. reg &= ~(PCXHR_IRQCS_ENABLE_PCIIRQ | PCXHR_IRQCS_ENABLE_PCIDB);
  220. PCXHR_OUTPL(mgr, PCXHR_PLX_IRQCS, reg);
  221. }
  222. void pcxhr_reset_dsp(struct pcxhr_mgr *mgr)
  223. {
  224. /* disable interrupts */
  225. pcxhr_enable_irq(mgr, 0);
  226. /* let's reset the DSP */
  227. PCXHR_OUTPB(mgr, PCXHR_DSP_RESET, 0);
  228. msleep( PCXHR_WAIT_DEFAULT ); /* wait 2 msec */
  229. PCXHR_OUTPB(mgr, PCXHR_DSP_RESET, 3);
  230. msleep( PCXHR_WAIT_DEFAULT ); /* wait 2 msec */
  231. /* reset mailbox */
  232. PCXHR_OUTPL(mgr, PCXHR_PLX_MBOX0, 0);
  233. }
  234. void pcxhr_enable_dsp(struct pcxhr_mgr *mgr)
  235. {
  236. /* enable interrupts */
  237. pcxhr_enable_irq(mgr, 1);
  238. }
  239. /*
  240. * load the xilinx image
  241. */
  242. int pcxhr_load_xilinx_binary(struct pcxhr_mgr *mgr, const struct firmware *xilinx, int second)
  243. {
  244. unsigned int i;
  245. unsigned int chipsc;
  246. unsigned char data;
  247. unsigned char mask;
  248. const unsigned char *image;
  249. /* test first xilinx */
  250. chipsc = PCXHR_INPL(mgr, PCXHR_PLX_CHIPSC);
  251. /* REV01 cards do not support the PCXHR_CHIPSC_GPI_USERI bit anymore */
  252. /* this bit will always be 1; no possibility to test presence of first xilinx */
  253. if(second) {
  254. if ((chipsc & PCXHR_CHIPSC_GPI_USERI) == 0) {
  255. snd_printk(KERN_ERR "error loading first xilinx\n");
  256. return -EINVAL;
  257. }
  258. /* activate second xilinx */
  259. chipsc |= PCXHR_CHIPSC_RESET_XILINX;
  260. PCXHR_OUTPL(mgr, PCXHR_PLX_CHIPSC, chipsc);
  261. msleep( PCXHR_WAIT_DEFAULT ); /* wait 2 msec */
  262. }
  263. image = xilinx->data;
  264. for (i = 0; i < xilinx->size; i++, image++) {
  265. data = *image;
  266. mask = 0x80;
  267. while (mask) {
  268. chipsc &= ~(PCXHR_CHIPSC_DATA_CLK | PCXHR_CHIPSC_DATA_IN);
  269. if (data & mask)
  270. chipsc |= PCXHR_CHIPSC_DATA_IN;
  271. PCXHR_OUTPL(mgr, PCXHR_PLX_CHIPSC, chipsc);
  272. chipsc |= PCXHR_CHIPSC_DATA_CLK;
  273. PCXHR_OUTPL(mgr, PCXHR_PLX_CHIPSC, chipsc);
  274. mask >>= 1;
  275. }
  276. /* don't take too much time in this loop... */
  277. cond_resched();
  278. }
  279. chipsc &= ~(PCXHR_CHIPSC_DATA_CLK | PCXHR_CHIPSC_DATA_IN);
  280. PCXHR_OUTPL(mgr, PCXHR_PLX_CHIPSC, chipsc);
  281. /* wait 2 msec (time to boot the xilinx before any access) */
  282. msleep( PCXHR_WAIT_DEFAULT );
  283. return 0;
  284. }
  285. /*
  286. * send an executable file to the DSP
  287. */
  288. static int pcxhr_download_dsp(struct pcxhr_mgr *mgr, const struct firmware *dsp)
  289. {
  290. int err;
  291. unsigned int i;
  292. unsigned int len;
  293. const unsigned char *data;
  294. unsigned char dummy;
  295. /* check the length of boot image */
  296. if (dsp->size <= 0)
  297. return -EINVAL;
  298. if (dsp->size % 3)
  299. return -EINVAL;
  300. if (snd_BUG_ON(!dsp->data))
  301. return -EINVAL;
  302. /* transfert data buffer from PC to DSP */
  303. for (i = 0; i < dsp->size; i += 3) {
  304. data = dsp->data + i;
  305. if (i == 0) {
  306. /* test data header consistency */
  307. len = (unsigned int)((data[0]<<16) + (data[1]<<8) + data[2]);
  308. if (len && dsp->size != (len + 2) * 3)
  309. return -EINVAL;
  310. }
  311. /* wait DSP ready for new transfer */
  312. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_TRDY,
  313. PCXHR_ISR_HI08_TRDY, PCXHR_TIMEOUT_DSP, &dummy);
  314. if (err) {
  315. snd_printk(KERN_ERR "dsp loading error at position %d\n", i);
  316. return err;
  317. }
  318. /* send host data */
  319. PCXHR_OUTPB(mgr, PCXHR_DSP_TXH, data[0]);
  320. PCXHR_OUTPB(mgr, PCXHR_DSP_TXM, data[1]);
  321. PCXHR_OUTPB(mgr, PCXHR_DSP_TXL, data[2]);
  322. /* don't take too much time in this loop... */
  323. cond_resched();
  324. }
  325. /* give some time to boot the DSP */
  326. msleep(PCXHR_WAIT_DEFAULT);
  327. return 0;
  328. }
  329. /*
  330. * load the eeprom image
  331. */
  332. int pcxhr_load_eeprom_binary(struct pcxhr_mgr *mgr, const struct firmware *eeprom)
  333. {
  334. int err;
  335. unsigned char reg;
  336. /* init value of the ICR register */
  337. reg = PCXHR_ICR_HI08_RREQ | PCXHR_ICR_HI08_TREQ | PCXHR_ICR_HI08_HDRQ;
  338. if (PCXHR_INPL(mgr, PCXHR_PLX_MBOX0) & PCXHR_MBOX0_BOOT_HERE) {
  339. /* no need to load the eeprom binary, but init the HI08 interface */
  340. PCXHR_OUTPB(mgr, PCXHR_DSP_ICR, reg | PCXHR_ICR_HI08_INIT);
  341. msleep(PCXHR_WAIT_DEFAULT);
  342. PCXHR_OUTPB(mgr, PCXHR_DSP_ICR, reg);
  343. msleep(PCXHR_WAIT_DEFAULT);
  344. snd_printdd("no need to load eeprom boot\n");
  345. return 0;
  346. }
  347. PCXHR_OUTPB(mgr, PCXHR_DSP_ICR, reg);
  348. err = pcxhr_download_dsp(mgr, eeprom);
  349. if (err)
  350. return err;
  351. /* wait for chk bit */
  352. return pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_CHK,
  353. PCXHR_ISR_HI08_CHK, PCXHR_TIMEOUT_DSP, &reg);
  354. }
  355. /*
  356. * load the boot image
  357. */
  358. int pcxhr_load_boot_binary(struct pcxhr_mgr *mgr, const struct firmware *boot)
  359. {
  360. int err;
  361. unsigned int physaddr = mgr->hostport.addr;
  362. unsigned char dummy;
  363. /* send the hostport address to the DSP (only the upper 24 bit !) */
  364. if (snd_BUG_ON(physaddr & 0xff))
  365. return -EINVAL;
  366. PCXHR_OUTPL(mgr, PCXHR_PLX_MBOX1, (physaddr >> 8));
  367. err = pcxhr_send_it_dsp(mgr, PCXHR_IT_DOWNLOAD_BOOT, 0);
  368. if (err)
  369. return err;
  370. /* clear hf5 bit */
  371. PCXHR_OUTPL(mgr, PCXHR_PLX_MBOX0,
  372. PCXHR_INPL(mgr, PCXHR_PLX_MBOX0) & ~PCXHR_MBOX0_HF5);
  373. err = pcxhr_download_dsp(mgr, boot);
  374. if (err)
  375. return err;
  376. /* wait for hf5 bit */
  377. return pcxhr_check_reg_bit(mgr, PCXHR_PLX_MBOX0, PCXHR_MBOX0_HF5,
  378. PCXHR_MBOX0_HF5, PCXHR_TIMEOUT_DSP, &dummy);
  379. }
  380. /*
  381. * load the final dsp image
  382. */
  383. int pcxhr_load_dsp_binary(struct pcxhr_mgr *mgr, const struct firmware *dsp)
  384. {
  385. int err;
  386. unsigned char dummy;
  387. err = pcxhr_send_it_dsp(mgr, PCXHR_IT_RESET_BOARD_FUNC, 0);
  388. if (err)
  389. return err;
  390. err = pcxhr_send_it_dsp(mgr, PCXHR_IT_DOWNLOAD_DSP, 0);
  391. if (err)
  392. return err;
  393. err = pcxhr_download_dsp(mgr, dsp);
  394. if (err)
  395. return err;
  396. /* wait for chk bit */
  397. return pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_CHK,
  398. PCXHR_ISR_HI08_CHK, PCXHR_TIMEOUT_DSP, &dummy);
  399. }
  400. struct pcxhr_cmd_info {
  401. u32 opcode; /* command word */
  402. u16 st_length; /* status length */
  403. u16 st_type; /* status type (RMH_SSIZE_XXX) */
  404. };
  405. /* RMH status type */
  406. enum {
  407. RMH_SSIZE_FIXED = 0, /* status size fix (st_length = 0..x) */
  408. RMH_SSIZE_ARG = 1, /* status size given in the LSB byte (used with st_length = 1) */
  409. RMH_SSIZE_MASK = 2, /* status size given in bitmask (used with st_length = 1) */
  410. };
  411. /*
  412. * Array of DSP commands
  413. */
  414. static struct pcxhr_cmd_info pcxhr_dsp_cmds[] = {
  415. [CMD_VERSION] = { 0x010000, 1, RMH_SSIZE_FIXED },
  416. [CMD_SUPPORTED] = { 0x020000, 4, RMH_SSIZE_FIXED },
  417. [CMD_TEST_IT] = { 0x040000, 1, RMH_SSIZE_FIXED },
  418. [CMD_SEND_IRQA] = { 0x070001, 0, RMH_SSIZE_FIXED },
  419. [CMD_ACCESS_IO_WRITE] = { 0x090000, 1, RMH_SSIZE_ARG },
  420. [CMD_ACCESS_IO_READ] = { 0x094000, 1, RMH_SSIZE_ARG },
  421. [CMD_ASYNC] = { 0x0a0000, 1, RMH_SSIZE_ARG },
  422. [CMD_MODIFY_CLOCK] = { 0x0d0000, 0, RMH_SSIZE_FIXED },
  423. [CMD_RESYNC_AUDIO_INPUTS] = { 0x0e0000, 0, RMH_SSIZE_FIXED },
  424. [CMD_GET_DSP_RESOURCES] = { 0x100000, 4, RMH_SSIZE_FIXED },
  425. [CMD_SET_TIMER_INTERRUPT] = { 0x110000, 0, RMH_SSIZE_FIXED },
  426. [CMD_RES_PIPE] = { 0x400000, 0, RMH_SSIZE_FIXED },
  427. [CMD_FREE_PIPE] = { 0x410000, 0, RMH_SSIZE_FIXED },
  428. [CMD_CONF_PIPE] = { 0x422101, 0, RMH_SSIZE_FIXED },
  429. [CMD_STOP_PIPE] = { 0x470004, 0, RMH_SSIZE_FIXED },
  430. [CMD_PIPE_SAMPLE_COUNT] = { 0x49a000, 2, RMH_SSIZE_FIXED },
  431. [CMD_CAN_START_PIPE] = { 0x4b0000, 1, RMH_SSIZE_FIXED },
  432. [CMD_START_STREAM] = { 0x802000, 0, RMH_SSIZE_FIXED },
  433. [CMD_STREAM_OUT_LEVEL_ADJUST] = { 0x822000, 0, RMH_SSIZE_FIXED },
  434. [CMD_STOP_STREAM] = { 0x832000, 0, RMH_SSIZE_FIXED },
  435. [CMD_UPDATE_R_BUFFERS] = { 0x840000, 0, RMH_SSIZE_FIXED },
  436. [CMD_FORMAT_STREAM_OUT] = { 0x860000, 0, RMH_SSIZE_FIXED },
  437. [CMD_FORMAT_STREAM_IN] = { 0x870000, 0, RMH_SSIZE_FIXED },
  438. [CMD_STREAM_SAMPLE_COUNT] = { 0x902000, 2, RMH_SSIZE_FIXED }, /* stat_len = nb_streams * 2 */
  439. [CMD_AUDIO_LEVEL_ADJUST] = { 0xc22000, 0, RMH_SSIZE_FIXED },
  440. };
  441. #ifdef CONFIG_SND_DEBUG_VERBOSE
  442. static char* cmd_names[] = {
  443. [CMD_VERSION] = "CMD_VERSION",
  444. [CMD_SUPPORTED] = "CMD_SUPPORTED",
  445. [CMD_TEST_IT] = "CMD_TEST_IT",
  446. [CMD_SEND_IRQA] = "CMD_SEND_IRQA",
  447. [CMD_ACCESS_IO_WRITE] = "CMD_ACCESS_IO_WRITE",
  448. [CMD_ACCESS_IO_READ] = "CMD_ACCESS_IO_READ",
  449. [CMD_ASYNC] = "CMD_ASYNC",
  450. [CMD_MODIFY_CLOCK] = "CMD_MODIFY_CLOCK",
  451. [CMD_RESYNC_AUDIO_INPUTS] = "CMD_RESYNC_AUDIO_INPUTS",
  452. [CMD_GET_DSP_RESOURCES] = "CMD_GET_DSP_RESOURCES",
  453. [CMD_SET_TIMER_INTERRUPT] = "CMD_SET_TIMER_INTERRUPT",
  454. [CMD_RES_PIPE] = "CMD_RES_PIPE",
  455. [CMD_FREE_PIPE] = "CMD_FREE_PIPE",
  456. [CMD_CONF_PIPE] = "CMD_CONF_PIPE",
  457. [CMD_STOP_PIPE] = "CMD_STOP_PIPE",
  458. [CMD_PIPE_SAMPLE_COUNT] = "CMD_PIPE_SAMPLE_COUNT",
  459. [CMD_CAN_START_PIPE] = "CMD_CAN_START_PIPE",
  460. [CMD_START_STREAM] = "CMD_START_STREAM",
  461. [CMD_STREAM_OUT_LEVEL_ADJUST] = "CMD_STREAM_OUT_LEVEL_ADJUST",
  462. [CMD_STOP_STREAM] = "CMD_STOP_STREAM",
  463. [CMD_UPDATE_R_BUFFERS] = "CMD_UPDATE_R_BUFFERS",
  464. [CMD_FORMAT_STREAM_OUT] = "CMD_FORMAT_STREAM_OUT",
  465. [CMD_FORMAT_STREAM_IN] = "CMD_FORMAT_STREAM_IN",
  466. [CMD_STREAM_SAMPLE_COUNT] = "CMD_STREAM_SAMPLE_COUNT",
  467. [CMD_AUDIO_LEVEL_ADJUST] = "CMD_AUDIO_LEVEL_ADJUST",
  468. };
  469. #endif
  470. static int pcxhr_read_rmh_status(struct pcxhr_mgr *mgr, struct pcxhr_rmh *rmh)
  471. {
  472. int err;
  473. int i;
  474. u32 data;
  475. u32 size_mask;
  476. unsigned char reg;
  477. int max_stat_len;
  478. if (rmh->stat_len < PCXHR_SIZE_MAX_STATUS)
  479. max_stat_len = PCXHR_SIZE_MAX_STATUS;
  480. else max_stat_len = rmh->stat_len;
  481. for (i = 0; i < rmh->stat_len; i++) {
  482. /* wait for receiver full */
  483. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_RXDF,
  484. PCXHR_ISR_HI08_RXDF, PCXHR_TIMEOUT_DSP, &reg);
  485. if (err) {
  486. snd_printk(KERN_ERR "ERROR RMH stat: ISR:RXDF=1 (ISR = %x; i=%d )\n",
  487. reg, i);
  488. return err;
  489. }
  490. /* read data */
  491. data = PCXHR_INPB(mgr, PCXHR_DSP_TXH) << 16;
  492. data |= PCXHR_INPB(mgr, PCXHR_DSP_TXM) << 8;
  493. data |= PCXHR_INPB(mgr, PCXHR_DSP_TXL);
  494. /* need to update rmh->stat_len on the fly ?? */
  495. if (i==0) {
  496. if (rmh->dsp_stat != RMH_SSIZE_FIXED) {
  497. if (rmh->dsp_stat == RMH_SSIZE_ARG) {
  498. rmh->stat_len = (u16)(data & 0x0000ff) + 1;
  499. data &= 0xffff00;
  500. } else {
  501. /* rmh->dsp_stat == RMH_SSIZE_MASK */
  502. rmh->stat_len = 1;
  503. size_mask = data;
  504. while (size_mask) {
  505. if (size_mask & 1)
  506. rmh->stat_len++;
  507. size_mask >>= 1;
  508. }
  509. }
  510. }
  511. }
  512. #ifdef CONFIG_SND_DEBUG_VERBOSE
  513. if (rmh->cmd_idx < CMD_LAST_INDEX)
  514. snd_printdd(" stat[%d]=%x\n", i, data);
  515. #endif
  516. if (i < max_stat_len)
  517. rmh->stat[i] = data;
  518. }
  519. if (rmh->stat_len > max_stat_len) {
  520. snd_printdd("PCXHR : rmh->stat_len=%x too big\n", rmh->stat_len);
  521. rmh->stat_len = max_stat_len;
  522. }
  523. return 0;
  524. }
  525. static int pcxhr_send_msg_nolock(struct pcxhr_mgr *mgr, struct pcxhr_rmh *rmh)
  526. {
  527. int err;
  528. int i;
  529. u32 data;
  530. unsigned char reg;
  531. if (snd_BUG_ON(rmh->cmd_len >= PCXHR_SIZE_MAX_CMD))
  532. return -EINVAL;
  533. err = pcxhr_send_it_dsp(mgr, PCXHR_IT_MESSAGE, 1);
  534. if (err) {
  535. snd_printk(KERN_ERR "pcxhr_send_message : ED_DSP_CRASHED\n");
  536. return err;
  537. }
  538. /* wait for chk bit */
  539. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_CHK,
  540. PCXHR_ISR_HI08_CHK, PCXHR_TIMEOUT_DSP, &reg);
  541. if (err)
  542. return err;
  543. /* reset irq chk */
  544. err = pcxhr_send_it_dsp(mgr, PCXHR_IT_RESET_CHK, 1);
  545. if (err)
  546. return err;
  547. /* wait for chk bit == 0*/
  548. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_CHK, 0,
  549. PCXHR_TIMEOUT_DSP, &reg);
  550. if (err)
  551. return err;
  552. data = rmh->cmd[0];
  553. if (rmh->cmd_len > 1)
  554. data |= 0x008000; /* MASK_MORE_THAN_1_WORD_COMMAND */
  555. else
  556. data &= 0xff7fff; /* MASK_1_WORD_COMMAND */
  557. #ifdef CONFIG_SND_DEBUG_VERBOSE
  558. if (rmh->cmd_idx < CMD_LAST_INDEX)
  559. snd_printdd("MSG cmd[0]=%x (%s)\n", data, cmd_names[rmh->cmd_idx]);
  560. #endif
  561. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_TRDY,
  562. PCXHR_ISR_HI08_TRDY, PCXHR_TIMEOUT_DSP, &reg);
  563. if (err)
  564. return err;
  565. PCXHR_OUTPB(mgr, PCXHR_DSP_TXH, (data>>16)&0xFF);
  566. PCXHR_OUTPB(mgr, PCXHR_DSP_TXM, (data>>8)&0xFF);
  567. PCXHR_OUTPB(mgr, PCXHR_DSP_TXL, (data&0xFF));
  568. if (rmh->cmd_len > 1) {
  569. /* send length */
  570. data = rmh->cmd_len - 1;
  571. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_TRDY,
  572. PCXHR_ISR_HI08_TRDY, PCXHR_TIMEOUT_DSP, &reg);
  573. if (err)
  574. return err;
  575. PCXHR_OUTPB(mgr, PCXHR_DSP_TXH, (data>>16)&0xFF);
  576. PCXHR_OUTPB(mgr, PCXHR_DSP_TXM, (data>>8)&0xFF);
  577. PCXHR_OUTPB(mgr, PCXHR_DSP_TXL, (data&0xFF));
  578. for (i=1; i < rmh->cmd_len; i++) {
  579. /* send other words */
  580. data = rmh->cmd[i];
  581. #ifdef CONFIG_SND_DEBUG_VERBOSE
  582. if (rmh->cmd_idx < CMD_LAST_INDEX)
  583. snd_printdd(" cmd[%d]=%x\n", i, data);
  584. #endif
  585. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR,
  586. PCXHR_ISR_HI08_TRDY,
  587. PCXHR_ISR_HI08_TRDY,
  588. PCXHR_TIMEOUT_DSP, &reg);
  589. if (err)
  590. return err;
  591. PCXHR_OUTPB(mgr, PCXHR_DSP_TXH, (data>>16)&0xFF);
  592. PCXHR_OUTPB(mgr, PCXHR_DSP_TXM, (data>>8)&0xFF);
  593. PCXHR_OUTPB(mgr, PCXHR_DSP_TXL, (data&0xFF));
  594. }
  595. }
  596. /* wait for chk bit */
  597. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_CHK,
  598. PCXHR_ISR_HI08_CHK, PCXHR_TIMEOUT_DSP, &reg);
  599. if (err)
  600. return err;
  601. /* test status ISR */
  602. if (reg & PCXHR_ISR_HI08_ERR) {
  603. /* ERROR, wait for receiver full */
  604. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_RXDF,
  605. PCXHR_ISR_HI08_RXDF, PCXHR_TIMEOUT_DSP, &reg);
  606. if (err) {
  607. snd_printk(KERN_ERR "ERROR RMH: ISR:RXDF=1 (ISR = %x)\n", reg);
  608. return err;
  609. }
  610. /* read error code */
  611. data = PCXHR_INPB(mgr, PCXHR_DSP_TXH) << 16;
  612. data |= PCXHR_INPB(mgr, PCXHR_DSP_TXM) << 8;
  613. data |= PCXHR_INPB(mgr, PCXHR_DSP_TXL);
  614. snd_printk(KERN_ERR "ERROR RMH(%d): 0x%x\n", rmh->cmd_idx, data);
  615. err = -EINVAL;
  616. } else {
  617. /* read the response data */
  618. err = pcxhr_read_rmh_status(mgr, rmh);
  619. }
  620. /* reset semaphore */
  621. if (pcxhr_send_it_dsp(mgr, PCXHR_IT_RESET_SEMAPHORE, 1) < 0)
  622. return -EIO;
  623. return err;
  624. }
  625. /**
  626. * pcxhr_init_rmh - initialize the RMH instance
  627. * @rmh: the rmh pointer to be initialized
  628. * @cmd: the rmh command to be set
  629. */
  630. void pcxhr_init_rmh(struct pcxhr_rmh *rmh, int cmd)
  631. {
  632. if (snd_BUG_ON(cmd >= CMD_LAST_INDEX))
  633. return;
  634. rmh->cmd[0] = pcxhr_dsp_cmds[cmd].opcode;
  635. rmh->cmd_len = 1;
  636. rmh->stat_len = pcxhr_dsp_cmds[cmd].st_length;
  637. rmh->dsp_stat = pcxhr_dsp_cmds[cmd].st_type;
  638. rmh->cmd_idx = cmd;
  639. }
  640. void pcxhr_set_pipe_cmd_params(struct pcxhr_rmh *rmh, int capture,
  641. unsigned int param1, unsigned int param2,
  642. unsigned int param3)
  643. {
  644. snd_BUG_ON(param1 > MASK_FIRST_FIELD);
  645. if (capture)
  646. rmh->cmd[0] |= 0x800; /* COMMAND_RECORD_MASK */
  647. if (param1)
  648. rmh->cmd[0] |= (param1 << FIELD_SIZE);
  649. if (param2) {
  650. snd_BUG_ON(param2 > MASK_FIRST_FIELD);
  651. rmh->cmd[0] |= param2;
  652. }
  653. if(param3) {
  654. snd_BUG_ON(param3 > MASK_DSP_WORD);
  655. rmh->cmd[1] = param3;
  656. rmh->cmd_len = 2;
  657. }
  658. }
  659. /*
  660. * pcxhr_send_msg - send a DSP message with spinlock
  661. * @rmh: the rmh record to send and receive
  662. *
  663. * returns 0 if successful, or a negative error code.
  664. */
  665. int pcxhr_send_msg(struct pcxhr_mgr *mgr, struct pcxhr_rmh *rmh)
  666. {
  667. unsigned long flags;
  668. int err;
  669. spin_lock_irqsave(&mgr->msg_lock, flags);
  670. err = pcxhr_send_msg_nolock(mgr, rmh);
  671. spin_unlock_irqrestore(&mgr->msg_lock, flags);
  672. return err;
  673. }
  674. static inline int pcxhr_pipes_running(struct pcxhr_mgr *mgr)
  675. {
  676. int start_mask = PCXHR_INPL(mgr, PCXHR_PLX_MBOX2);
  677. /* least segnificant 12 bits are the pipe states for the playback audios */
  678. /* next 12 bits are the pipe states for the capture audios
  679. * (PCXHR_PIPE_STATE_CAPTURE_OFFSET)
  680. */
  681. start_mask &= 0xffffff;
  682. snd_printdd("CMD_PIPE_STATE MBOX2=0x%06x\n", start_mask);
  683. return start_mask;
  684. }
  685. #define PCXHR_PIPE_STATE_CAPTURE_OFFSET 12
  686. #define MAX_WAIT_FOR_DSP 20
  687. static int pcxhr_prepair_pipe_start(struct pcxhr_mgr *mgr, int audio_mask, int *retry)
  688. {
  689. struct pcxhr_rmh rmh;
  690. int err;
  691. int audio = 0;
  692. *retry = 0;
  693. while (audio_mask) {
  694. if (audio_mask & 1) {
  695. pcxhr_init_rmh(&rmh, CMD_CAN_START_PIPE);
  696. if (audio < PCXHR_PIPE_STATE_CAPTURE_OFFSET) {
  697. /* can start playback pipe */
  698. pcxhr_set_pipe_cmd_params(&rmh, 0, audio, 0, 0);
  699. } else {
  700. /* can start capture pipe */
  701. pcxhr_set_pipe_cmd_params(&rmh, 1, audio -
  702. PCXHR_PIPE_STATE_CAPTURE_OFFSET,
  703. 0, 0);
  704. }
  705. err = pcxhr_send_msg(mgr, &rmh);
  706. if (err) {
  707. snd_printk(KERN_ERR
  708. "error pipe start (CMD_CAN_START_PIPE) err=%x!\n",
  709. err);
  710. return err;
  711. }
  712. /* if the pipe couldn't be prepaired for start, retry it later */
  713. if (rmh.stat[0] == 0)
  714. *retry |= (1<<audio);
  715. }
  716. audio_mask>>=1;
  717. audio++;
  718. }
  719. return 0;
  720. }
  721. static int pcxhr_stop_pipes(struct pcxhr_mgr *mgr, int audio_mask)
  722. {
  723. struct pcxhr_rmh rmh;
  724. int err;
  725. int audio = 0;
  726. while (audio_mask) {
  727. if (audio_mask & 1) {
  728. pcxhr_init_rmh(&rmh, CMD_STOP_PIPE);
  729. if (audio < PCXHR_PIPE_STATE_CAPTURE_OFFSET) {
  730. /* stop playback pipe */
  731. pcxhr_set_pipe_cmd_params(&rmh, 0, audio, 0, 0);
  732. } else {
  733. /* stop capture pipe */
  734. pcxhr_set_pipe_cmd_params(&rmh, 1, audio -
  735. PCXHR_PIPE_STATE_CAPTURE_OFFSET,
  736. 0, 0);
  737. }
  738. err = pcxhr_send_msg(mgr, &rmh);
  739. if (err) {
  740. snd_printk(KERN_ERR
  741. "error pipe stop (CMD_STOP_PIPE) err=%x!\n",
  742. err);
  743. return err;
  744. }
  745. }
  746. audio_mask>>=1;
  747. audio++;
  748. }
  749. return 0;
  750. }
  751. static int pcxhr_toggle_pipes(struct pcxhr_mgr *mgr, int audio_mask)
  752. {
  753. struct pcxhr_rmh rmh;
  754. int err;
  755. int audio = 0;
  756. while (audio_mask) {
  757. if (audio_mask & 1) {
  758. pcxhr_init_rmh(&rmh, CMD_CONF_PIPE);
  759. if (audio < PCXHR_PIPE_STATE_CAPTURE_OFFSET)
  760. pcxhr_set_pipe_cmd_params(&rmh, 0, 0, 0, 1 << audio);
  761. else
  762. pcxhr_set_pipe_cmd_params(&rmh, 1, 0, 0,
  763. 1 << (audio - PCXHR_PIPE_STATE_CAPTURE_OFFSET));
  764. err = pcxhr_send_msg(mgr, &rmh);
  765. if (err) {
  766. snd_printk(KERN_ERR
  767. "error pipe start (CMD_CONF_PIPE) err=%x!\n",
  768. err);
  769. return err;
  770. }
  771. }
  772. audio_mask>>=1;
  773. audio++;
  774. }
  775. /* now fire the interrupt on the card */
  776. pcxhr_init_rmh(&rmh, CMD_SEND_IRQA);
  777. err = pcxhr_send_msg(mgr, &rmh);
  778. if (err) {
  779. snd_printk(KERN_ERR "error pipe start (CMD_SEND_IRQA) err=%x!\n", err );
  780. return err;
  781. }
  782. return 0;
  783. }
  784. int pcxhr_set_pipe_state(struct pcxhr_mgr *mgr, int playback_mask, int capture_mask, int start)
  785. {
  786. int state, i, err;
  787. int audio_mask;
  788. #ifdef CONFIG_SND_DEBUG_VERBOSE
  789. struct timeval my_tv1, my_tv2;
  790. do_gettimeofday(&my_tv1);
  791. #endif
  792. audio_mask = (playback_mask | (capture_mask << PCXHR_PIPE_STATE_CAPTURE_OFFSET));
  793. /* current pipe state (playback + record) */
  794. state = pcxhr_pipes_running(mgr);
  795. snd_printdd("pcxhr_set_pipe_state %s (mask %x current %x)\n",
  796. start ? "START" : "STOP", audio_mask, state);
  797. if (start) {
  798. audio_mask &= ~state; /* start only pipes that are not yet started */
  799. state = audio_mask;
  800. for (i = 0; i < MAX_WAIT_FOR_DSP; i++) {
  801. err = pcxhr_prepair_pipe_start(mgr, state, &state);
  802. if (err)
  803. return err;
  804. if (state == 0)
  805. break; /* success, all pipes prepaired for start */
  806. mdelay(1); /* otherwise wait 1 millisecond and retry */
  807. }
  808. } else {
  809. audio_mask &= state; /* stop only pipes that are started */
  810. }
  811. if (audio_mask == 0)
  812. return 0;
  813. err = pcxhr_toggle_pipes(mgr, audio_mask);
  814. if (err)
  815. return err;
  816. i = 0;
  817. while (1) {
  818. state = pcxhr_pipes_running(mgr);
  819. /* have all pipes the new state ? */
  820. if ((state & audio_mask) == (start ? audio_mask : 0))
  821. break;
  822. if (++i >= MAX_WAIT_FOR_DSP * 100) {
  823. snd_printk(KERN_ERR "error pipe start/stop (ED_NO_RESPONSE_AT_IRQA)\n");
  824. return -EBUSY;
  825. }
  826. udelay(10); /* wait 10 microseconds */
  827. }
  828. if (!start) {
  829. err = pcxhr_stop_pipes(mgr, audio_mask);
  830. if (err)
  831. return err;
  832. }
  833. #ifdef CONFIG_SND_DEBUG_VERBOSE
  834. do_gettimeofday(&my_tv2);
  835. snd_printdd("***SET PIPE STATE*** TIME = %ld (err = %x)\n",
  836. (long)(my_tv2.tv_usec - my_tv1.tv_usec), err);
  837. #endif
  838. return 0;
  839. }
  840. int pcxhr_write_io_num_reg_cont(struct pcxhr_mgr *mgr, unsigned int mask,
  841. unsigned int value, int *changed)
  842. {
  843. struct pcxhr_rmh rmh;
  844. unsigned long flags;
  845. int err;
  846. spin_lock_irqsave(&mgr->msg_lock, flags);
  847. if ((mgr->io_num_reg_cont & mask) == value) {
  848. snd_printdd("IO_NUM_REG_CONT mask %x already is set to %x\n", mask, value);
  849. if (changed)
  850. *changed = 0;
  851. spin_unlock_irqrestore(&mgr->msg_lock, flags);
  852. return 0; /* already programmed */
  853. }
  854. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
  855. rmh.cmd[0] |= IO_NUM_REG_CONT;
  856. rmh.cmd[1] = mask;
  857. rmh.cmd[2] = value;
  858. rmh.cmd_len = 3;
  859. err = pcxhr_send_msg_nolock(mgr, &rmh);
  860. if (err == 0) {
  861. mgr->io_num_reg_cont &= ~mask;
  862. mgr->io_num_reg_cont |= value;
  863. if (changed)
  864. *changed = 1;
  865. }
  866. spin_unlock_irqrestore(&mgr->msg_lock, flags);
  867. return err;
  868. }
  869. #define PCXHR_IRQ_TIMER 0x000300
  870. #define PCXHR_IRQ_FREQ_CHANGE 0x000800
  871. #define PCXHR_IRQ_TIME_CODE 0x001000
  872. #define PCXHR_IRQ_NOTIFY 0x002000
  873. #define PCXHR_IRQ_ASYNC 0x008000
  874. #define PCXHR_IRQ_MASK 0x00bb00
  875. #define PCXHR_FATAL_DSP_ERR 0xff0000
  876. enum pcxhr_async_err_src {
  877. PCXHR_ERR_PIPE,
  878. PCXHR_ERR_STREAM,
  879. PCXHR_ERR_AUDIO
  880. };
  881. static int pcxhr_handle_async_err(struct pcxhr_mgr *mgr, u32 err,
  882. enum pcxhr_async_err_src err_src, int pipe,
  883. int is_capture)
  884. {
  885. #ifdef CONFIG_SND_DEBUG_VERBOSE
  886. static char* err_src_name[] = {
  887. [PCXHR_ERR_PIPE] = "Pipe",
  888. [PCXHR_ERR_STREAM] = "Stream",
  889. [PCXHR_ERR_AUDIO] = "Audio"
  890. };
  891. #endif
  892. if (err & 0xfff)
  893. err &= 0xfff;
  894. else
  895. err = ((err >> 12) & 0xfff);
  896. if (!err)
  897. return 0;
  898. snd_printdd("CMD_ASYNC : Error %s %s Pipe %d err=%x\n", err_src_name[err_src],
  899. is_capture ? "Record" : "Play", pipe, err);
  900. if (err == 0xe01)
  901. mgr->async_err_stream_xrun++;
  902. else if (err == 0xe10)
  903. mgr->async_err_pipe_xrun++;
  904. else
  905. mgr->async_err_other_last = (int)err;
  906. return 1;
  907. }
  908. void pcxhr_msg_tasklet(unsigned long arg)
  909. {
  910. struct pcxhr_mgr *mgr = (struct pcxhr_mgr *)(arg);
  911. struct pcxhr_rmh *prmh = mgr->prmh;
  912. int err;
  913. int i, j;
  914. if (mgr->src_it_dsp & PCXHR_IRQ_FREQ_CHANGE)
  915. snd_printdd("TASKLET : PCXHR_IRQ_FREQ_CHANGE event occured\n");
  916. if (mgr->src_it_dsp & PCXHR_IRQ_TIME_CODE)
  917. snd_printdd("TASKLET : PCXHR_IRQ_TIME_CODE event occured\n");
  918. if (mgr->src_it_dsp & PCXHR_IRQ_NOTIFY)
  919. snd_printdd("TASKLET : PCXHR_IRQ_NOTIFY event occured\n");
  920. if (mgr->src_it_dsp & PCXHR_IRQ_ASYNC) {
  921. snd_printdd("TASKLET : PCXHR_IRQ_ASYNC event occured\n");
  922. pcxhr_init_rmh(prmh, CMD_ASYNC);
  923. prmh->cmd[0] |= 1; /* add SEL_ASYNC_EVENTS */
  924. /* this is the only one extra long response command */
  925. prmh->stat_len = PCXHR_SIZE_MAX_LONG_STATUS;
  926. err = pcxhr_send_msg(mgr, prmh);
  927. if (err)
  928. snd_printk(KERN_ERR "ERROR pcxhr_msg_tasklet=%x;\n", err);
  929. i = 1;
  930. while (i < prmh->stat_len) {
  931. int nb_audio = (prmh->stat[i] >> FIELD_SIZE) & MASK_FIRST_FIELD;
  932. int nb_stream = (prmh->stat[i] >> (2*FIELD_SIZE)) & MASK_FIRST_FIELD;
  933. int pipe = prmh->stat[i] & MASK_FIRST_FIELD;
  934. int is_capture = prmh->stat[i] & 0x400000;
  935. u32 err2;
  936. if (prmh->stat[i] & 0x800000) { /* if BIT_END */
  937. snd_printdd("TASKLET : End%sPipe %d\n",
  938. is_capture ? "Record" : "Play", pipe);
  939. }
  940. i++;
  941. err2 = prmh->stat[i] ? prmh->stat[i] : prmh->stat[i+1];
  942. if (err2)
  943. pcxhr_handle_async_err(mgr, err2,
  944. PCXHR_ERR_PIPE,
  945. pipe, is_capture);
  946. i += 2;
  947. for (j = 0; j < nb_stream; j++) {
  948. err2 = prmh->stat[i] ?
  949. prmh->stat[i] : prmh->stat[i+1];
  950. if (err2)
  951. pcxhr_handle_async_err(mgr, err2,
  952. PCXHR_ERR_STREAM,
  953. pipe,
  954. is_capture);
  955. i += 2;
  956. }
  957. for (j = 0; j < nb_audio; j++) {
  958. err2 = prmh->stat[i] ?
  959. prmh->stat[i] : prmh->stat[i+1];
  960. if (err2)
  961. pcxhr_handle_async_err(mgr, err2,
  962. PCXHR_ERR_AUDIO,
  963. pipe,
  964. is_capture);
  965. i += 2;
  966. }
  967. }
  968. }
  969. }
  970. static u_int64_t pcxhr_stream_read_position(struct pcxhr_mgr *mgr,
  971. struct pcxhr_stream *stream)
  972. {
  973. u_int64_t hw_sample_count;
  974. struct pcxhr_rmh rmh;
  975. int err, stream_mask;
  976. stream_mask = stream->pipe->is_capture ? 1 : 1<<stream->substream->number;
  977. /* get sample count for one stream */
  978. pcxhr_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
  979. pcxhr_set_pipe_cmd_params(&rmh, stream->pipe->is_capture,
  980. stream->pipe->first_audio, 0, stream_mask);
  981. /* rmh.stat_len = 2; */ /* 2 resp data for each stream of the pipe */
  982. err = pcxhr_send_msg(mgr, &rmh);
  983. if (err)
  984. return 0;
  985. hw_sample_count = ((u_int64_t)rmh.stat[0]) << 24;
  986. hw_sample_count += (u_int64_t)rmh.stat[1];
  987. snd_printdd("stream %c%d : abs samples real(%ld) timer(%ld)\n",
  988. stream->pipe->is_capture ? 'C':'P', stream->substream->number,
  989. (long unsigned int)hw_sample_count,
  990. (long unsigned int)(stream->timer_abs_periods +
  991. stream->timer_period_frag + PCXHR_GRANULARITY));
  992. return hw_sample_count;
  993. }
  994. static void pcxhr_update_timer_pos(struct pcxhr_mgr *mgr,
  995. struct pcxhr_stream *stream, int samples_to_add)
  996. {
  997. if (stream->substream && (stream->status == PCXHR_STREAM_STATUS_RUNNING)) {
  998. u_int64_t new_sample_count;
  999. int elapsed = 0;
  1000. int hardware_read = 0;
  1001. struct snd_pcm_runtime *runtime = stream->substream->runtime;
  1002. if (samples_to_add < 0) {
  1003. stream->timer_is_synced = 0;
  1004. /* add default if no hardware_read possible */
  1005. samples_to_add = PCXHR_GRANULARITY;
  1006. }
  1007. if (!stream->timer_is_synced) {
  1008. if (stream->timer_abs_periods != 0 ||
  1009. stream->timer_period_frag + PCXHR_GRANULARITY >=
  1010. runtime->period_size) {
  1011. new_sample_count = pcxhr_stream_read_position(mgr, stream);
  1012. hardware_read = 1;
  1013. if (new_sample_count >= PCXHR_GRANULARITY_MIN) {
  1014. /* sub security offset because of jitter and
  1015. * finer granularity of dsp time (MBOX4)
  1016. */
  1017. new_sample_count -= PCXHR_GRANULARITY_MIN;
  1018. stream->timer_is_synced = 1;
  1019. }
  1020. }
  1021. }
  1022. if (!hardware_read) {
  1023. /* if we didn't try to sync the position, increment it
  1024. * by PCXHR_GRANULARITY every timer interrupt
  1025. */
  1026. new_sample_count = stream->timer_abs_periods +
  1027. stream->timer_period_frag + samples_to_add;
  1028. }
  1029. while (1) {
  1030. u_int64_t new_elapse_pos = stream->timer_abs_periods +
  1031. runtime->period_size;
  1032. if (new_elapse_pos > new_sample_count)
  1033. break;
  1034. elapsed = 1;
  1035. stream->timer_buf_periods++;
  1036. if (stream->timer_buf_periods >= runtime->periods)
  1037. stream->timer_buf_periods = 0;
  1038. stream->timer_abs_periods = new_elapse_pos;
  1039. }
  1040. if (new_sample_count >= stream->timer_abs_periods)
  1041. stream->timer_period_frag = (u_int32_t)(new_sample_count -
  1042. stream->timer_abs_periods);
  1043. else
  1044. snd_printk(KERN_ERR "ERROR new_sample_count too small ??? %lx\n",
  1045. (long unsigned int)new_sample_count);
  1046. if (elapsed) {
  1047. spin_unlock(&mgr->lock);
  1048. snd_pcm_period_elapsed(stream->substream);
  1049. spin_lock(&mgr->lock);
  1050. }
  1051. }
  1052. }
  1053. irqreturn_t pcxhr_interrupt(int irq, void *dev_id)
  1054. {
  1055. struct pcxhr_mgr *mgr = dev_id;
  1056. unsigned int reg;
  1057. int i, j;
  1058. struct snd_pcxhr *chip;
  1059. spin_lock(&mgr->lock);
  1060. reg = PCXHR_INPL(mgr, PCXHR_PLX_IRQCS);
  1061. if (! (reg & PCXHR_IRQCS_ACTIVE_PCIDB)) {
  1062. spin_unlock(&mgr->lock);
  1063. return IRQ_NONE; /* this device did not cause the interrupt */
  1064. }
  1065. /* clear interrupt */
  1066. reg = PCXHR_INPL(mgr, PCXHR_PLX_L2PCIDB);
  1067. PCXHR_OUTPL(mgr, PCXHR_PLX_L2PCIDB, reg);
  1068. /* timer irq occured */
  1069. if (reg & PCXHR_IRQ_TIMER) {
  1070. int timer_toggle = reg & PCXHR_IRQ_TIMER;
  1071. /* is a 24 bit counter */
  1072. int dsp_time_new = PCXHR_INPL(mgr, PCXHR_PLX_MBOX4) & PCXHR_DSP_TIME_MASK;
  1073. int dsp_time_diff = dsp_time_new - mgr->dsp_time_last;
  1074. if (dsp_time_diff < 0 && mgr->dsp_time_last != PCXHR_DSP_TIME_INVALID) {
  1075. snd_printdd("ERROR DSP TIME old(%d) new(%d) -> "
  1076. "resynchronize all streams\n",
  1077. mgr->dsp_time_last, dsp_time_new);
  1078. mgr->dsp_time_err++;
  1079. }
  1080. #ifdef CONFIG_SND_DEBUG_VERBOSE
  1081. if (dsp_time_diff == 0)
  1082. snd_printdd("ERROR DSP TIME NO DIFF time(%d)\n", dsp_time_new);
  1083. else if (dsp_time_diff >= (2*PCXHR_GRANULARITY))
  1084. snd_printdd("ERROR DSP TIME TOO BIG old(%d) add(%d)\n",
  1085. mgr->dsp_time_last, dsp_time_new - mgr->dsp_time_last);
  1086. #endif
  1087. mgr->dsp_time_last = dsp_time_new;
  1088. if (timer_toggle == mgr->timer_toggle)
  1089. snd_printdd("ERROR TIMER TOGGLE\n");
  1090. mgr->timer_toggle = timer_toggle;
  1091. reg &= ~PCXHR_IRQ_TIMER;
  1092. for (i = 0; i < mgr->num_cards; i++) {
  1093. chip = mgr->chip[i];
  1094. for (j = 0; j < chip->nb_streams_capt; j++)
  1095. pcxhr_update_timer_pos(mgr, &chip->capture_stream[j],
  1096. dsp_time_diff);
  1097. }
  1098. for (i = 0; i < mgr->num_cards; i++) {
  1099. chip = mgr->chip[i];
  1100. for (j = 0; j < chip->nb_streams_play; j++)
  1101. pcxhr_update_timer_pos(mgr, &chip->playback_stream[j],
  1102. dsp_time_diff);
  1103. }
  1104. }
  1105. /* other irq's handled in the tasklet */
  1106. if (reg & PCXHR_IRQ_MASK) {
  1107. /* as we didn't request any notifications, some kind of xrun error
  1108. * will probably occured
  1109. */
  1110. /* better resynchronize all streams next interrupt : */
  1111. mgr->dsp_time_last = PCXHR_DSP_TIME_INVALID;
  1112. mgr->src_it_dsp = reg;
  1113. tasklet_hi_schedule(&mgr->msg_taskq);
  1114. }
  1115. #ifdef CONFIG_SND_DEBUG_VERBOSE
  1116. if (reg & PCXHR_FATAL_DSP_ERR)
  1117. snd_printdd("FATAL DSP ERROR : %x\n", reg);
  1118. #endif
  1119. spin_unlock(&mgr->lock);
  1120. return IRQ_HANDLED; /* this device caused the interrupt */
  1121. }