pcxhr_core.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. 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. unsigned char *data;
  294. unsigned char dummy;
  295. /* check the length of boot image */
  296. snd_assert(dsp->size > 0, return -EINVAL);
  297. snd_assert(dsp->size % 3 == 0, return -EINVAL);
  298. snd_assert(dsp->data, return -EINVAL);
  299. /* transfert data buffer from PC to DSP */
  300. for (i = 0; i < dsp->size; i += 3) {
  301. data = dsp->data + i;
  302. if (i == 0) {
  303. /* test data header consistency */
  304. len = (unsigned int)((data[0]<<16) + (data[1]<<8) + data[2]);
  305. snd_assert((len==0) || (dsp->size == (len+2)*3), return -EINVAL);
  306. }
  307. /* wait DSP ready for new transfer */
  308. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_TRDY,
  309. PCXHR_ISR_HI08_TRDY, PCXHR_TIMEOUT_DSP, &dummy);
  310. if (err) {
  311. snd_printk(KERN_ERR "dsp loading error at position %d\n", i);
  312. return err;
  313. }
  314. /* send host data */
  315. PCXHR_OUTPB(mgr, PCXHR_DSP_TXH, data[0]);
  316. PCXHR_OUTPB(mgr, PCXHR_DSP_TXM, data[1]);
  317. PCXHR_OUTPB(mgr, PCXHR_DSP_TXL, data[2]);
  318. /* don't take too much time in this loop... */
  319. cond_resched();
  320. }
  321. /* give some time to boot the DSP */
  322. msleep(PCXHR_WAIT_DEFAULT);
  323. return 0;
  324. }
  325. /*
  326. * load the eeprom image
  327. */
  328. int pcxhr_load_eeprom_binary(struct pcxhr_mgr *mgr, const struct firmware *eeprom)
  329. {
  330. int err;
  331. unsigned char reg;
  332. /* init value of the ICR register */
  333. reg = PCXHR_ICR_HI08_RREQ | PCXHR_ICR_HI08_TREQ | PCXHR_ICR_HI08_HDRQ;
  334. if (PCXHR_INPL(mgr, PCXHR_PLX_MBOX0) & PCXHR_MBOX0_BOOT_HERE) {
  335. /* no need to load the eeprom binary, but init the HI08 interface */
  336. PCXHR_OUTPB(mgr, PCXHR_DSP_ICR, reg | PCXHR_ICR_HI08_INIT);
  337. msleep(PCXHR_WAIT_DEFAULT);
  338. PCXHR_OUTPB(mgr, PCXHR_DSP_ICR, reg);
  339. msleep(PCXHR_WAIT_DEFAULT);
  340. snd_printdd("no need to load eeprom boot\n");
  341. return 0;
  342. }
  343. PCXHR_OUTPB(mgr, PCXHR_DSP_ICR, reg);
  344. err = pcxhr_download_dsp(mgr, eeprom);
  345. if (err)
  346. return err;
  347. /* wait for chk bit */
  348. return pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_CHK,
  349. PCXHR_ISR_HI08_CHK, PCXHR_TIMEOUT_DSP, &reg);
  350. }
  351. /*
  352. * load the boot image
  353. */
  354. int pcxhr_load_boot_binary(struct pcxhr_mgr *mgr, const struct firmware *boot)
  355. {
  356. int err;
  357. unsigned int physaddr = mgr->hostport.addr;
  358. unsigned char dummy;
  359. /* send the hostport address to the DSP (only the upper 24 bit !) */
  360. snd_assert((physaddr & 0xff) == 0, return -EINVAL);
  361. PCXHR_OUTPL(mgr, PCXHR_PLX_MBOX1, (physaddr >> 8));
  362. err = pcxhr_send_it_dsp(mgr, PCXHR_IT_DOWNLOAD_BOOT, 0);
  363. if (err)
  364. return err;
  365. /* clear hf5 bit */
  366. PCXHR_OUTPL(mgr, PCXHR_PLX_MBOX0,
  367. PCXHR_INPL(mgr, PCXHR_PLX_MBOX0) & ~PCXHR_MBOX0_HF5);
  368. err = pcxhr_download_dsp(mgr, boot);
  369. if (err)
  370. return err;
  371. /* wait for hf5 bit */
  372. return pcxhr_check_reg_bit(mgr, PCXHR_PLX_MBOX0, PCXHR_MBOX0_HF5,
  373. PCXHR_MBOX0_HF5, PCXHR_TIMEOUT_DSP, &dummy);
  374. }
  375. /*
  376. * load the final dsp image
  377. */
  378. int pcxhr_load_dsp_binary(struct pcxhr_mgr *mgr, const struct firmware *dsp)
  379. {
  380. int err;
  381. unsigned char dummy;
  382. err = pcxhr_send_it_dsp(mgr, PCXHR_IT_RESET_BOARD_FUNC, 0);
  383. if (err)
  384. return err;
  385. err = pcxhr_send_it_dsp(mgr, PCXHR_IT_DOWNLOAD_DSP, 0);
  386. if (err)
  387. return err;
  388. err = pcxhr_download_dsp(mgr, dsp);
  389. if (err)
  390. return err;
  391. /* wait for chk bit */
  392. return pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_CHK,
  393. PCXHR_ISR_HI08_CHK, PCXHR_TIMEOUT_DSP, &dummy);
  394. }
  395. struct pcxhr_cmd_info {
  396. u32 opcode; /* command word */
  397. u16 st_length; /* status length */
  398. u16 st_type; /* status type (RMH_SSIZE_XXX) */
  399. };
  400. /* RMH status type */
  401. enum {
  402. RMH_SSIZE_FIXED = 0, /* status size fix (st_length = 0..x) */
  403. RMH_SSIZE_ARG = 1, /* status size given in the LSB byte (used with st_length = 1) */
  404. RMH_SSIZE_MASK = 2, /* status size given in bitmask (used with st_length = 1) */
  405. };
  406. /*
  407. * Array of DSP commands
  408. */
  409. static struct pcxhr_cmd_info pcxhr_dsp_cmds[] = {
  410. [CMD_VERSION] = { 0x010000, 1, RMH_SSIZE_FIXED },
  411. [CMD_SUPPORTED] = { 0x020000, 4, RMH_SSIZE_FIXED },
  412. [CMD_TEST_IT] = { 0x040000, 1, RMH_SSIZE_FIXED },
  413. [CMD_SEND_IRQA] = { 0x070001, 0, RMH_SSIZE_FIXED },
  414. [CMD_ACCESS_IO_WRITE] = { 0x090000, 1, RMH_SSIZE_ARG },
  415. [CMD_ACCESS_IO_READ] = { 0x094000, 1, RMH_SSIZE_ARG },
  416. [CMD_ASYNC] = { 0x0a0000, 1, RMH_SSIZE_ARG },
  417. [CMD_MODIFY_CLOCK] = { 0x0d0000, 0, RMH_SSIZE_FIXED },
  418. [CMD_RESYNC_AUDIO_INPUTS] = { 0x0e0000, 0, RMH_SSIZE_FIXED },
  419. [CMD_GET_DSP_RESOURCES] = { 0x100000, 4, RMH_SSIZE_FIXED },
  420. [CMD_SET_TIMER_INTERRUPT] = { 0x110000, 0, RMH_SSIZE_FIXED },
  421. [CMD_RES_PIPE] = { 0x400000, 0, RMH_SSIZE_FIXED },
  422. [CMD_FREE_PIPE] = { 0x410000, 0, RMH_SSIZE_FIXED },
  423. [CMD_CONF_PIPE] = { 0x422101, 0, RMH_SSIZE_FIXED },
  424. [CMD_STOP_PIPE] = { 0x470004, 0, RMH_SSIZE_FIXED },
  425. [CMD_PIPE_SAMPLE_COUNT] = { 0x49a000, 2, RMH_SSIZE_FIXED },
  426. [CMD_CAN_START_PIPE] = { 0x4b0000, 1, RMH_SSIZE_FIXED },
  427. [CMD_START_STREAM] = { 0x802000, 0, RMH_SSIZE_FIXED },
  428. [CMD_STREAM_OUT_LEVEL_ADJUST] = { 0x822000, 0, RMH_SSIZE_FIXED },
  429. [CMD_STOP_STREAM] = { 0x832000, 0, RMH_SSIZE_FIXED },
  430. [CMD_UPDATE_R_BUFFERS] = { 0x840000, 0, RMH_SSIZE_FIXED },
  431. [CMD_FORMAT_STREAM_OUT] = { 0x860000, 0, RMH_SSIZE_FIXED },
  432. [CMD_FORMAT_STREAM_IN] = { 0x870000, 0, RMH_SSIZE_FIXED },
  433. [CMD_STREAM_SAMPLE_COUNT] = { 0x902000, 2, RMH_SSIZE_FIXED }, /* stat_len = nb_streams * 2 */
  434. [CMD_AUDIO_LEVEL_ADJUST] = { 0xc22000, 0, RMH_SSIZE_FIXED },
  435. };
  436. #ifdef CONFIG_SND_DEBUG_DETECT
  437. static char* cmd_names[] = {
  438. [CMD_VERSION] = "CMD_VERSION",
  439. [CMD_SUPPORTED] = "CMD_SUPPORTED",
  440. [CMD_TEST_IT] = "CMD_TEST_IT",
  441. [CMD_SEND_IRQA] = "CMD_SEND_IRQA",
  442. [CMD_ACCESS_IO_WRITE] = "CMD_ACCESS_IO_WRITE",
  443. [CMD_ACCESS_IO_READ] = "CMD_ACCESS_IO_READ",
  444. [CMD_ASYNC] = "CMD_ASYNC",
  445. [CMD_MODIFY_CLOCK] = "CMD_MODIFY_CLOCK",
  446. [CMD_RESYNC_AUDIO_INPUTS] = "CMD_RESYNC_AUDIO_INPUTS",
  447. [CMD_GET_DSP_RESOURCES] = "CMD_GET_DSP_RESOURCES",
  448. [CMD_SET_TIMER_INTERRUPT] = "CMD_SET_TIMER_INTERRUPT",
  449. [CMD_RES_PIPE] = "CMD_RES_PIPE",
  450. [CMD_FREE_PIPE] = "CMD_FREE_PIPE",
  451. [CMD_CONF_PIPE] = "CMD_CONF_PIPE",
  452. [CMD_STOP_PIPE] = "CMD_STOP_PIPE",
  453. [CMD_PIPE_SAMPLE_COUNT] = "CMD_PIPE_SAMPLE_COUNT",
  454. [CMD_CAN_START_PIPE] = "CMD_CAN_START_PIPE",
  455. [CMD_START_STREAM] = "CMD_START_STREAM",
  456. [CMD_STREAM_OUT_LEVEL_ADJUST] = "CMD_STREAM_OUT_LEVEL_ADJUST",
  457. [CMD_STOP_STREAM] = "CMD_STOP_STREAM",
  458. [CMD_UPDATE_R_BUFFERS] = "CMD_UPDATE_R_BUFFERS",
  459. [CMD_FORMAT_STREAM_OUT] = "CMD_FORMAT_STREAM_OUT",
  460. [CMD_FORMAT_STREAM_IN] = "CMD_FORMAT_STREAM_IN",
  461. [CMD_STREAM_SAMPLE_COUNT] = "CMD_STREAM_SAMPLE_COUNT",
  462. [CMD_AUDIO_LEVEL_ADJUST] = "CMD_AUDIO_LEVEL_ADJUST",
  463. };
  464. #endif
  465. static int pcxhr_read_rmh_status(struct pcxhr_mgr *mgr, struct pcxhr_rmh *rmh)
  466. {
  467. int err;
  468. int i;
  469. u32 data;
  470. u32 size_mask;
  471. unsigned char reg;
  472. int max_stat_len;
  473. if (rmh->stat_len < PCXHR_SIZE_MAX_STATUS)
  474. max_stat_len = PCXHR_SIZE_MAX_STATUS;
  475. else max_stat_len = rmh->stat_len;
  476. for (i = 0; i < rmh->stat_len; i++) {
  477. /* wait for receiver full */
  478. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_RXDF,
  479. PCXHR_ISR_HI08_RXDF, PCXHR_TIMEOUT_DSP, &reg);
  480. if (err) {
  481. snd_printk(KERN_ERR "ERROR RMH stat: ISR:RXDF=1 (ISR = %x; i=%d )\n",
  482. reg, i);
  483. return err;
  484. }
  485. /* read data */
  486. data = PCXHR_INPB(mgr, PCXHR_DSP_TXH) << 16;
  487. data |= PCXHR_INPB(mgr, PCXHR_DSP_TXM) << 8;
  488. data |= PCXHR_INPB(mgr, PCXHR_DSP_TXL);
  489. /* need to update rmh->stat_len on the fly ?? */
  490. if (i==0) {
  491. if (rmh->dsp_stat != RMH_SSIZE_FIXED) {
  492. if (rmh->dsp_stat == RMH_SSIZE_ARG) {
  493. rmh->stat_len = (u16)(data & 0x0000ff) + 1;
  494. data &= 0xffff00;
  495. } else {
  496. /* rmh->dsp_stat == RMH_SSIZE_MASK */
  497. rmh->stat_len = 1;
  498. size_mask = data;
  499. while (size_mask) {
  500. if (size_mask & 1)
  501. rmh->stat_len++;
  502. size_mask >>= 1;
  503. }
  504. }
  505. }
  506. }
  507. #ifdef CONFIG_SND_DEBUG_DETECT
  508. if (rmh->cmd_idx < CMD_LAST_INDEX)
  509. snd_printdd(" stat[%d]=%x\n", i, data);
  510. #endif
  511. if (i < max_stat_len)
  512. rmh->stat[i] = data;
  513. }
  514. if (rmh->stat_len > max_stat_len) {
  515. snd_printdd("PCXHR : rmh->stat_len=%x too big\n", rmh->stat_len);
  516. rmh->stat_len = max_stat_len;
  517. }
  518. return 0;
  519. }
  520. static int pcxhr_send_msg_nolock(struct pcxhr_mgr *mgr, struct pcxhr_rmh *rmh)
  521. {
  522. int err;
  523. int i;
  524. u32 data;
  525. unsigned char reg;
  526. snd_assert(rmh->cmd_len<PCXHR_SIZE_MAX_CMD, return -EINVAL);
  527. err = pcxhr_send_it_dsp(mgr, PCXHR_IT_MESSAGE, 1);
  528. if (err) {
  529. snd_printk(KERN_ERR "pcxhr_send_message : ED_DSP_CRASHED\n");
  530. return err;
  531. }
  532. /* wait for chk bit */
  533. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_CHK,
  534. PCXHR_ISR_HI08_CHK, PCXHR_TIMEOUT_DSP, &reg);
  535. if (err)
  536. return err;
  537. /* reset irq chk */
  538. err = pcxhr_send_it_dsp(mgr, PCXHR_IT_RESET_CHK, 1);
  539. if (err)
  540. return err;
  541. /* wait for chk bit == 0*/
  542. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_CHK, 0,
  543. PCXHR_TIMEOUT_DSP, &reg);
  544. if (err)
  545. return err;
  546. data = rmh->cmd[0];
  547. if (rmh->cmd_len > 1)
  548. data |= 0x008000; /* MASK_MORE_THAN_1_WORD_COMMAND */
  549. else
  550. data &= 0xff7fff; /* MASK_1_WORD_COMMAND */
  551. #ifdef CONFIG_SND_DEBUG_DETECT
  552. if (rmh->cmd_idx < CMD_LAST_INDEX)
  553. snd_printdd("MSG cmd[0]=%x (%s)\n", data, cmd_names[rmh->cmd_idx]);
  554. #endif
  555. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_TRDY,
  556. PCXHR_ISR_HI08_TRDY, PCXHR_TIMEOUT_DSP, &reg);
  557. if (err)
  558. return err;
  559. PCXHR_OUTPB(mgr, PCXHR_DSP_TXH, (data>>16)&0xFF);
  560. PCXHR_OUTPB(mgr, PCXHR_DSP_TXM, (data>>8)&0xFF);
  561. PCXHR_OUTPB(mgr, PCXHR_DSP_TXL, (data&0xFF));
  562. if (rmh->cmd_len > 1) {
  563. /* send length */
  564. data = rmh->cmd_len - 1;
  565. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_TRDY,
  566. PCXHR_ISR_HI08_TRDY, PCXHR_TIMEOUT_DSP, &reg);
  567. if (err)
  568. return err;
  569. PCXHR_OUTPB(mgr, PCXHR_DSP_TXH, (data>>16)&0xFF);
  570. PCXHR_OUTPB(mgr, PCXHR_DSP_TXM, (data>>8)&0xFF);
  571. PCXHR_OUTPB(mgr, PCXHR_DSP_TXL, (data&0xFF));
  572. for (i=1; i < rmh->cmd_len; i++) {
  573. /* send other words */
  574. data = rmh->cmd[i];
  575. #ifdef CONFIG_SND_DEBUG_DETECT
  576. if (rmh->cmd_idx < CMD_LAST_INDEX)
  577. snd_printdd(" cmd[%d]=%x\n", i, data);
  578. #endif
  579. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR,
  580. PCXHR_ISR_HI08_TRDY,
  581. PCXHR_ISR_HI08_TRDY,
  582. PCXHR_TIMEOUT_DSP, &reg);
  583. if (err)
  584. return err;
  585. PCXHR_OUTPB(mgr, PCXHR_DSP_TXH, (data>>16)&0xFF);
  586. PCXHR_OUTPB(mgr, PCXHR_DSP_TXM, (data>>8)&0xFF);
  587. PCXHR_OUTPB(mgr, PCXHR_DSP_TXL, (data&0xFF));
  588. }
  589. }
  590. /* wait for chk bit */
  591. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_CHK,
  592. PCXHR_ISR_HI08_CHK, PCXHR_TIMEOUT_DSP, &reg);
  593. if (err)
  594. return err;
  595. /* test status ISR */
  596. if (reg & PCXHR_ISR_HI08_ERR) {
  597. /* ERROR, wait for receiver full */
  598. err = pcxhr_check_reg_bit(mgr, PCXHR_DSP_ISR, PCXHR_ISR_HI08_RXDF,
  599. PCXHR_ISR_HI08_RXDF, PCXHR_TIMEOUT_DSP, &reg);
  600. if (err) {
  601. snd_printk(KERN_ERR "ERROR RMH: ISR:RXDF=1 (ISR = %x)\n", reg);
  602. return err;
  603. }
  604. /* read error code */
  605. data = PCXHR_INPB(mgr, PCXHR_DSP_TXH) << 16;
  606. data |= PCXHR_INPB(mgr, PCXHR_DSP_TXM) << 8;
  607. data |= PCXHR_INPB(mgr, PCXHR_DSP_TXL);
  608. snd_printk(KERN_ERR "ERROR RMH(%d): 0x%x\n", rmh->cmd_idx, data);
  609. err = -EINVAL;
  610. } else {
  611. /* read the response data */
  612. err = pcxhr_read_rmh_status(mgr, rmh);
  613. }
  614. /* reset semaphore */
  615. if (pcxhr_send_it_dsp(mgr, PCXHR_IT_RESET_SEMAPHORE, 1) < 0)
  616. return -EIO;
  617. return err;
  618. }
  619. /**
  620. * pcxhr_init_rmh - initialize the RMH instance
  621. * @rmh: the rmh pointer to be initialized
  622. * @cmd: the rmh command to be set
  623. */
  624. void pcxhr_init_rmh(struct pcxhr_rmh *rmh, int cmd)
  625. {
  626. snd_assert(cmd < CMD_LAST_INDEX, return);
  627. rmh->cmd[0] = pcxhr_dsp_cmds[cmd].opcode;
  628. rmh->cmd_len = 1;
  629. rmh->stat_len = pcxhr_dsp_cmds[cmd].st_length;
  630. rmh->dsp_stat = pcxhr_dsp_cmds[cmd].st_type;
  631. rmh->cmd_idx = cmd;
  632. }
  633. void pcxhr_set_pipe_cmd_params(struct pcxhr_rmh *rmh, int capture,
  634. unsigned int param1, unsigned int param2,
  635. unsigned int param3)
  636. {
  637. snd_assert(param1 <= MASK_FIRST_FIELD);
  638. if (capture)
  639. rmh->cmd[0] |= 0x800; /* COMMAND_RECORD_MASK */
  640. if (param1)
  641. rmh->cmd[0] |= (param1 << FIELD_SIZE);
  642. if (param2) {
  643. snd_assert(param2 <= MASK_FIRST_FIELD);
  644. rmh->cmd[0] |= param2;
  645. }
  646. if(param3) {
  647. snd_assert(param3 <= MASK_DSP_WORD);
  648. rmh->cmd[1] = param3;
  649. rmh->cmd_len = 2;
  650. }
  651. }
  652. /*
  653. * pcxhr_send_msg - send a DSP message with spinlock
  654. * @rmh: the rmh record to send and receive
  655. *
  656. * returns 0 if successful, or a negative error code.
  657. */
  658. int pcxhr_send_msg(struct pcxhr_mgr *mgr, struct pcxhr_rmh *rmh)
  659. {
  660. unsigned long flags;
  661. int err;
  662. spin_lock_irqsave(&mgr->msg_lock, flags);
  663. err = pcxhr_send_msg_nolock(mgr, rmh);
  664. spin_unlock_irqrestore(&mgr->msg_lock, flags);
  665. return err;
  666. }
  667. static inline int pcxhr_pipes_running(struct pcxhr_mgr *mgr)
  668. {
  669. int start_mask = PCXHR_INPL(mgr, PCXHR_PLX_MBOX2);
  670. /* least segnificant 12 bits are the pipe states for the playback audios */
  671. /* next 12 bits are the pipe states for the capture audios
  672. * (PCXHR_PIPE_STATE_CAPTURE_OFFSET)
  673. */
  674. start_mask &= 0xffffff;
  675. snd_printdd("CMD_PIPE_STATE MBOX2=0x%06x\n", start_mask);
  676. return start_mask;
  677. }
  678. #define PCXHR_PIPE_STATE_CAPTURE_OFFSET 12
  679. #define MAX_WAIT_FOR_DSP 20
  680. static int pcxhr_prepair_pipe_start(struct pcxhr_mgr *mgr, int audio_mask, int *retry)
  681. {
  682. struct pcxhr_rmh rmh;
  683. int err;
  684. int audio = 0;
  685. *retry = 0;
  686. while (audio_mask) {
  687. if (audio_mask & 1) {
  688. pcxhr_init_rmh(&rmh, CMD_CAN_START_PIPE);
  689. if (audio < PCXHR_PIPE_STATE_CAPTURE_OFFSET) {
  690. /* can start playback pipe */
  691. pcxhr_set_pipe_cmd_params(&rmh, 0, audio, 0, 0);
  692. } else {
  693. /* can start capture pipe */
  694. pcxhr_set_pipe_cmd_params(&rmh, 1, audio -
  695. PCXHR_PIPE_STATE_CAPTURE_OFFSET,
  696. 0, 0);
  697. }
  698. err = pcxhr_send_msg(mgr, &rmh);
  699. if (err) {
  700. snd_printk(KERN_ERR
  701. "error pipe start (CMD_CAN_START_PIPE) err=%x!\n",
  702. err);
  703. return err;
  704. }
  705. /* if the pipe couldn't be prepaired for start, retry it later */
  706. if (rmh.stat[0] == 0)
  707. *retry |= (1<<audio);
  708. }
  709. audio_mask>>=1;
  710. audio++;
  711. }
  712. return 0;
  713. }
  714. static int pcxhr_stop_pipes(struct pcxhr_mgr *mgr, int audio_mask)
  715. {
  716. struct pcxhr_rmh rmh;
  717. int err;
  718. int audio = 0;
  719. while (audio_mask) {
  720. if (audio_mask & 1) {
  721. pcxhr_init_rmh(&rmh, CMD_STOP_PIPE);
  722. if (audio < PCXHR_PIPE_STATE_CAPTURE_OFFSET) {
  723. /* stop playback pipe */
  724. pcxhr_set_pipe_cmd_params(&rmh, 0, audio, 0, 0);
  725. } else {
  726. /* stop capture pipe */
  727. pcxhr_set_pipe_cmd_params(&rmh, 1, audio -
  728. PCXHR_PIPE_STATE_CAPTURE_OFFSET,
  729. 0, 0);
  730. }
  731. err = pcxhr_send_msg(mgr, &rmh);
  732. if (err) {
  733. snd_printk(KERN_ERR
  734. "error pipe stop (CMD_STOP_PIPE) err=%x!\n",
  735. err);
  736. return err;
  737. }
  738. }
  739. audio_mask>>=1;
  740. audio++;
  741. }
  742. return 0;
  743. }
  744. static int pcxhr_toggle_pipes(struct pcxhr_mgr *mgr, int audio_mask)
  745. {
  746. struct pcxhr_rmh rmh;
  747. int err;
  748. int audio = 0;
  749. while (audio_mask) {
  750. if (audio_mask & 1) {
  751. pcxhr_init_rmh(&rmh, CMD_CONF_PIPE);
  752. if (audio < PCXHR_PIPE_STATE_CAPTURE_OFFSET)
  753. pcxhr_set_pipe_cmd_params(&rmh, 0, 0, 0, 1 << audio);
  754. else
  755. pcxhr_set_pipe_cmd_params(&rmh, 1, 0, 0,
  756. 1 << (audio - PCXHR_PIPE_STATE_CAPTURE_OFFSET));
  757. err = pcxhr_send_msg(mgr, &rmh);
  758. if (err) {
  759. snd_printk(KERN_ERR
  760. "error pipe start (CMD_CONF_PIPE) err=%x!\n",
  761. err);
  762. return err;
  763. }
  764. }
  765. audio_mask>>=1;
  766. audio++;
  767. }
  768. /* now fire the interrupt on the card */
  769. pcxhr_init_rmh(&rmh, CMD_SEND_IRQA);
  770. err = pcxhr_send_msg(mgr, &rmh);
  771. if (err) {
  772. snd_printk(KERN_ERR "error pipe start (CMD_SEND_IRQA) err=%x!\n", err );
  773. return err;
  774. }
  775. return 0;
  776. }
  777. int pcxhr_set_pipe_state(struct pcxhr_mgr *mgr, int playback_mask, int capture_mask, int start)
  778. {
  779. int state, i, err;
  780. int audio_mask;
  781. #ifdef CONFIG_SND_DEBUG_DETECT
  782. struct timeval my_tv1, my_tv2;
  783. do_gettimeofday(&my_tv1);
  784. #endif
  785. audio_mask = (playback_mask | (capture_mask << PCXHR_PIPE_STATE_CAPTURE_OFFSET));
  786. /* current pipe state (playback + record) */
  787. state = pcxhr_pipes_running(mgr);
  788. snd_printdd("pcxhr_set_pipe_state %s (mask %x current %x)\n",
  789. start ? "START" : "STOP", audio_mask, state);
  790. if (start) {
  791. audio_mask &= ~state; /* start only pipes that are not yet started */
  792. state = audio_mask;
  793. for (i = 0; i < MAX_WAIT_FOR_DSP; i++) {
  794. err = pcxhr_prepair_pipe_start(mgr, state, &state);
  795. if (err)
  796. return err;
  797. if (state == 0)
  798. break; /* success, all pipes prepaired for start */
  799. mdelay(1); /* otherwise wait 1 millisecond and retry */
  800. }
  801. } else {
  802. audio_mask &= state; /* stop only pipes that are started */
  803. }
  804. if (audio_mask == 0)
  805. return 0;
  806. err = pcxhr_toggle_pipes(mgr, audio_mask);
  807. if (err)
  808. return err;
  809. i = 0;
  810. while (1) {
  811. state = pcxhr_pipes_running(mgr);
  812. /* have all pipes the new state ? */
  813. if ((state & audio_mask) == (start ? audio_mask : 0))
  814. break;
  815. if (++i >= MAX_WAIT_FOR_DSP * 100) {
  816. snd_printk(KERN_ERR "error pipe start/stop (ED_NO_RESPONSE_AT_IRQA)\n");
  817. return -EBUSY;
  818. }
  819. udelay(10); /* wait 10 microseconds */
  820. }
  821. if (!start) {
  822. err = pcxhr_stop_pipes(mgr, audio_mask);
  823. if (err)
  824. return err;
  825. }
  826. #ifdef CONFIG_SND_DEBUG_DETECT
  827. do_gettimeofday(&my_tv2);
  828. snd_printdd("***SET PIPE STATE*** TIME = %ld (err = %x)\n",
  829. my_tv2.tv_usec - my_tv1.tv_usec, err);
  830. #endif
  831. return 0;
  832. }
  833. int pcxhr_write_io_num_reg_cont(struct pcxhr_mgr *mgr, unsigned int mask,
  834. unsigned int value, int *changed)
  835. {
  836. struct pcxhr_rmh rmh;
  837. unsigned long flags;
  838. int err;
  839. spin_lock_irqsave(&mgr->msg_lock, flags);
  840. if ((mgr->io_num_reg_cont & mask) == value) {
  841. snd_printdd("IO_NUM_REG_CONT mask %x already is set to %x\n", mask, value);
  842. if (changed)
  843. *changed = 0;
  844. spin_unlock_irqrestore(&mgr->msg_lock, flags);
  845. return 0; /* already programmed */
  846. }
  847. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
  848. rmh.cmd[0] |= IO_NUM_REG_CONT;
  849. rmh.cmd[1] = mask;
  850. rmh.cmd[2] = value;
  851. rmh.cmd_len = 3;
  852. err = pcxhr_send_msg_nolock(mgr, &rmh);
  853. if (err == 0) {
  854. mgr->io_num_reg_cont &= ~mask;
  855. mgr->io_num_reg_cont |= value;
  856. if (changed)
  857. *changed = 1;
  858. }
  859. spin_unlock_irqrestore(&mgr->msg_lock, flags);
  860. return err;
  861. }
  862. #define PCXHR_IRQ_TIMER 0x000300
  863. #define PCXHR_IRQ_FREQ_CHANGE 0x000800
  864. #define PCXHR_IRQ_TIME_CODE 0x001000
  865. #define PCXHR_IRQ_NOTIFY 0x002000
  866. #define PCXHR_IRQ_ASYNC 0x008000
  867. #define PCXHR_IRQ_MASK 0x00bb00
  868. #define PCXHR_FATAL_DSP_ERR 0xff0000
  869. enum pcxhr_async_err_src {
  870. PCXHR_ERR_PIPE,
  871. PCXHR_ERR_STREAM,
  872. PCXHR_ERR_AUDIO
  873. };
  874. static int pcxhr_handle_async_err(struct pcxhr_mgr *mgr, u32 err,
  875. enum pcxhr_async_err_src err_src, int pipe,
  876. int is_capture)
  877. {
  878. #ifdef CONFIG_SND_DEBUG_DETECT
  879. static char* err_src_name[] = {
  880. [PCXHR_ERR_PIPE] = "Pipe",
  881. [PCXHR_ERR_STREAM] = "Stream",
  882. [PCXHR_ERR_AUDIO] = "Audio"
  883. };
  884. #endif
  885. if (err & 0xfff)
  886. err &= 0xfff;
  887. else
  888. err = ((err >> 12) & 0xfff);
  889. if (!err)
  890. return 0;
  891. snd_printdd("CMD_ASYNC : Error %s %s Pipe %d err=%x\n", err_src_name[err_src],
  892. is_capture ? "Record" : "Play", pipe, err);
  893. if (err == 0xe01)
  894. mgr->async_err_stream_xrun++;
  895. else if (err == 0xe10)
  896. mgr->async_err_pipe_xrun++;
  897. else
  898. mgr->async_err_other_last = (int)err;
  899. return 1;
  900. }
  901. void pcxhr_msg_tasklet(unsigned long arg)
  902. {
  903. struct pcxhr_mgr *mgr = (struct pcxhr_mgr *)(arg);
  904. struct pcxhr_rmh *prmh = mgr->prmh;
  905. int err;
  906. int i, j;
  907. if (mgr->src_it_dsp & PCXHR_IRQ_FREQ_CHANGE)
  908. snd_printdd("TASKLET : PCXHR_IRQ_FREQ_CHANGE event occured\n");
  909. if (mgr->src_it_dsp & PCXHR_IRQ_TIME_CODE)
  910. snd_printdd("TASKLET : PCXHR_IRQ_TIME_CODE event occured\n");
  911. if (mgr->src_it_dsp & PCXHR_IRQ_NOTIFY)
  912. snd_printdd("TASKLET : PCXHR_IRQ_NOTIFY event occured\n");
  913. if (mgr->src_it_dsp & PCXHR_IRQ_ASYNC) {
  914. snd_printdd("TASKLET : PCXHR_IRQ_ASYNC event occured\n");
  915. pcxhr_init_rmh(prmh, CMD_ASYNC);
  916. prmh->cmd[0] |= 1; /* add SEL_ASYNC_EVENTS */
  917. /* this is the only one extra long response command */
  918. prmh->stat_len = PCXHR_SIZE_MAX_LONG_STATUS;
  919. err = pcxhr_send_msg(mgr, prmh);
  920. if (err)
  921. snd_printk(KERN_ERR "ERROR pcxhr_msg_tasklet=%x;\n", err);
  922. i = 1;
  923. while (i < prmh->stat_len) {
  924. int nb_audio = (prmh->stat[i] >> FIELD_SIZE) & MASK_FIRST_FIELD;
  925. int nb_stream = (prmh->stat[i] >> (2*FIELD_SIZE)) & MASK_FIRST_FIELD;
  926. int pipe = prmh->stat[i] & MASK_FIRST_FIELD;
  927. int is_capture = prmh->stat[i] & 0x400000;
  928. u32 err;
  929. if (prmh->stat[i] & 0x800000) { /* if BIT_END */
  930. snd_printdd("TASKLET : End%sPipe %d\n",
  931. is_capture ? "Record" : "Play", pipe);
  932. }
  933. i++;
  934. err = prmh->stat[i] ? prmh->stat[i] : prmh->stat[i+1];
  935. if (err)
  936. pcxhr_handle_async_err(mgr, err, PCXHR_ERR_PIPE,
  937. pipe, is_capture);
  938. i += 2;
  939. for (j = 0; j < nb_stream; j++) {
  940. err = prmh->stat[i] ? prmh->stat[i] : prmh->stat[i+1];
  941. if (err)
  942. pcxhr_handle_async_err(mgr, err, PCXHR_ERR_STREAM,
  943. pipe, is_capture);
  944. i += 2;
  945. }
  946. for (j = 0; j < nb_audio; j++) {
  947. err = prmh->stat[i] ? prmh->stat[i] : prmh->stat[i+1];
  948. if (err)
  949. pcxhr_handle_async_err(mgr, err, PCXHR_ERR_AUDIO,
  950. pipe, is_capture);
  951. i += 2;
  952. }
  953. }
  954. }
  955. }
  956. static u_int64_t pcxhr_stream_read_position(struct pcxhr_mgr *mgr,
  957. struct pcxhr_stream *stream)
  958. {
  959. u_int64_t hw_sample_count;
  960. struct pcxhr_rmh rmh;
  961. int err, stream_mask;
  962. stream_mask = stream->pipe->is_capture ? 1 : 1<<stream->substream->number;
  963. /* get sample count for one stream */
  964. pcxhr_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
  965. pcxhr_set_pipe_cmd_params(&rmh, stream->pipe->is_capture,
  966. stream->pipe->first_audio, 0, stream_mask);
  967. /* rmh.stat_len = 2; */ /* 2 resp data for each stream of the pipe */
  968. err = pcxhr_send_msg(mgr, &rmh);
  969. if (err)
  970. return 0;
  971. hw_sample_count = ((u_int64_t)rmh.stat[0]) << 24;
  972. hw_sample_count += (u_int64_t)rmh.stat[1];
  973. snd_printdd("stream %c%d : abs samples real(%ld) timer(%ld)\n",
  974. stream->pipe->is_capture ? 'C':'P', stream->substream->number,
  975. (long unsigned int)hw_sample_count,
  976. (long unsigned int)(stream->timer_abs_periods +
  977. stream->timer_period_frag + PCXHR_GRANULARITY));
  978. return hw_sample_count;
  979. }
  980. static void pcxhr_update_timer_pos(struct pcxhr_mgr *mgr,
  981. struct pcxhr_stream *stream, int samples_to_add)
  982. {
  983. if (stream->substream && (stream->status == PCXHR_STREAM_STATUS_RUNNING)) {
  984. u_int64_t new_sample_count;
  985. int elapsed = 0;
  986. int hardware_read = 0;
  987. struct snd_pcm_runtime *runtime = stream->substream->runtime;
  988. if (samples_to_add < 0) {
  989. stream->timer_is_synced = 0;
  990. /* add default if no hardware_read possible */
  991. samples_to_add = PCXHR_GRANULARITY;
  992. }
  993. if (!stream->timer_is_synced) {
  994. if (stream->timer_abs_periods != 0 ||
  995. stream->timer_period_frag + PCXHR_GRANULARITY >=
  996. runtime->period_size) {
  997. new_sample_count = pcxhr_stream_read_position(mgr, stream);
  998. hardware_read = 1;
  999. if (new_sample_count >= PCXHR_GRANULARITY_MIN) {
  1000. /* sub security offset because of jitter and
  1001. * finer granularity of dsp time (MBOX4)
  1002. */
  1003. new_sample_count -= PCXHR_GRANULARITY_MIN;
  1004. stream->timer_is_synced = 1;
  1005. }
  1006. }
  1007. }
  1008. if (!hardware_read) {
  1009. /* if we didn't try to sync the position, increment it
  1010. * by PCXHR_GRANULARITY every timer interrupt
  1011. */
  1012. new_sample_count = stream->timer_abs_periods +
  1013. stream->timer_period_frag + samples_to_add;
  1014. }
  1015. while (1) {
  1016. u_int64_t new_elapse_pos = stream->timer_abs_periods +
  1017. runtime->period_size;
  1018. if (new_elapse_pos > new_sample_count)
  1019. break;
  1020. elapsed = 1;
  1021. stream->timer_buf_periods++;
  1022. if (stream->timer_buf_periods >= runtime->periods)
  1023. stream->timer_buf_periods = 0;
  1024. stream->timer_abs_periods = new_elapse_pos;
  1025. }
  1026. if (new_sample_count >= stream->timer_abs_periods)
  1027. stream->timer_period_frag = (u_int32_t)(new_sample_count -
  1028. stream->timer_abs_periods);
  1029. else
  1030. snd_printk(KERN_ERR "ERROR new_sample_count too small ??? %lx\n",
  1031. (long unsigned int)new_sample_count);
  1032. if (elapsed) {
  1033. spin_unlock(&mgr->lock);
  1034. snd_pcm_period_elapsed(stream->substream);
  1035. spin_lock(&mgr->lock);
  1036. }
  1037. }
  1038. }
  1039. irqreturn_t pcxhr_interrupt(int irq, void *dev_id)
  1040. {
  1041. struct pcxhr_mgr *mgr = dev_id;
  1042. unsigned int reg;
  1043. int i, j;
  1044. struct snd_pcxhr *chip;
  1045. spin_lock(&mgr->lock);
  1046. reg = PCXHR_INPL(mgr, PCXHR_PLX_IRQCS);
  1047. if (! (reg & PCXHR_IRQCS_ACTIVE_PCIDB)) {
  1048. spin_unlock(&mgr->lock);
  1049. return IRQ_NONE; /* this device did not cause the interrupt */
  1050. }
  1051. /* clear interrupt */
  1052. reg = PCXHR_INPL(mgr, PCXHR_PLX_L2PCIDB);
  1053. PCXHR_OUTPL(mgr, PCXHR_PLX_L2PCIDB, reg);
  1054. /* timer irq occured */
  1055. if (reg & PCXHR_IRQ_TIMER) {
  1056. int timer_toggle = reg & PCXHR_IRQ_TIMER;
  1057. /* is a 24 bit counter */
  1058. int dsp_time_new = PCXHR_INPL(mgr, PCXHR_PLX_MBOX4) & PCXHR_DSP_TIME_MASK;
  1059. int dsp_time_diff = dsp_time_new - mgr->dsp_time_last;
  1060. if (dsp_time_diff < 0 && mgr->dsp_time_last != PCXHR_DSP_TIME_INVALID) {
  1061. snd_printdd("ERROR DSP TIME old(%d) new(%d) -> "
  1062. "resynchronize all streams\n",
  1063. mgr->dsp_time_last, dsp_time_new);
  1064. mgr->dsp_time_err++;
  1065. }
  1066. #ifdef CONFIG_SND_DEBUG_DETECT
  1067. if (dsp_time_diff == 0)
  1068. snd_printdd("ERROR DSP TIME NO DIFF time(%d)\n", dsp_time_new);
  1069. else if (dsp_time_diff >= (2*PCXHR_GRANULARITY))
  1070. snd_printdd("ERROR DSP TIME TOO BIG old(%d) add(%d)\n",
  1071. mgr->dsp_time_last, dsp_time_new - mgr->dsp_time_last);
  1072. #endif
  1073. mgr->dsp_time_last = dsp_time_new;
  1074. if (timer_toggle == mgr->timer_toggle)
  1075. snd_printdd("ERROR TIMER TOGGLE\n");
  1076. mgr->timer_toggle = timer_toggle;
  1077. reg &= ~PCXHR_IRQ_TIMER;
  1078. for (i = 0; i < mgr->num_cards; i++) {
  1079. chip = mgr->chip[i];
  1080. for (j = 0; j < chip->nb_streams_capt; j++)
  1081. pcxhr_update_timer_pos(mgr, &chip->capture_stream[j],
  1082. dsp_time_diff);
  1083. }
  1084. for (i = 0; i < mgr->num_cards; i++) {
  1085. chip = mgr->chip[i];
  1086. for (j = 0; j < chip->nb_streams_play; j++)
  1087. pcxhr_update_timer_pos(mgr, &chip->playback_stream[j],
  1088. dsp_time_diff);
  1089. }
  1090. }
  1091. /* other irq's handled in the tasklet */
  1092. if (reg & PCXHR_IRQ_MASK) {
  1093. /* as we didn't request any notifications, some kind of xrun error
  1094. * will probably occured
  1095. */
  1096. /* better resynchronize all streams next interrupt : */
  1097. mgr->dsp_time_last = PCXHR_DSP_TIME_INVALID;
  1098. mgr->src_it_dsp = reg;
  1099. tasklet_hi_schedule(&mgr->msg_taskq);
  1100. }
  1101. #ifdef CONFIG_SND_DEBUG_DETECT
  1102. if (reg & PCXHR_FATAL_DSP_ERR)
  1103. snd_printdd("FATAL DSP ERROR : %x\n", reg);
  1104. #endif
  1105. spin_unlock(&mgr->lock);
  1106. return IRQ_HANDLED; /* this device caused the interrupt */
  1107. }