pcxhr_core.c 36 KB

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