pcxhr_core.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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. (long)(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 err2;
  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. err2 = prmh->stat[i] ? prmh->stat[i] : prmh->stat[i+1];
  935. if (err2)
  936. pcxhr_handle_async_err(mgr, err2,
  937. PCXHR_ERR_PIPE,
  938. pipe, is_capture);
  939. i += 2;
  940. for (j = 0; j < nb_stream; j++) {
  941. err2 = prmh->stat[i] ?
  942. prmh->stat[i] : prmh->stat[i+1];
  943. if (err2)
  944. pcxhr_handle_async_err(mgr, err2,
  945. PCXHR_ERR_STREAM,
  946. pipe,
  947. is_capture);
  948. i += 2;
  949. }
  950. for (j = 0; j < nb_audio; j++) {
  951. err2 = prmh->stat[i] ?
  952. prmh->stat[i] : prmh->stat[i+1];
  953. if (err2)
  954. pcxhr_handle_async_err(mgr, err2,
  955. PCXHR_ERR_AUDIO,
  956. pipe,
  957. is_capture);
  958. i += 2;
  959. }
  960. }
  961. }
  962. }
  963. static u_int64_t pcxhr_stream_read_position(struct pcxhr_mgr *mgr,
  964. struct pcxhr_stream *stream)
  965. {
  966. u_int64_t hw_sample_count;
  967. struct pcxhr_rmh rmh;
  968. int err, stream_mask;
  969. stream_mask = stream->pipe->is_capture ? 1 : 1<<stream->substream->number;
  970. /* get sample count for one stream */
  971. pcxhr_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
  972. pcxhr_set_pipe_cmd_params(&rmh, stream->pipe->is_capture,
  973. stream->pipe->first_audio, 0, stream_mask);
  974. /* rmh.stat_len = 2; */ /* 2 resp data for each stream of the pipe */
  975. err = pcxhr_send_msg(mgr, &rmh);
  976. if (err)
  977. return 0;
  978. hw_sample_count = ((u_int64_t)rmh.stat[0]) << 24;
  979. hw_sample_count += (u_int64_t)rmh.stat[1];
  980. snd_printdd("stream %c%d : abs samples real(%ld) timer(%ld)\n",
  981. stream->pipe->is_capture ? 'C':'P', stream->substream->number,
  982. (long unsigned int)hw_sample_count,
  983. (long unsigned int)(stream->timer_abs_periods +
  984. stream->timer_period_frag + PCXHR_GRANULARITY));
  985. return hw_sample_count;
  986. }
  987. static void pcxhr_update_timer_pos(struct pcxhr_mgr *mgr,
  988. struct pcxhr_stream *stream, int samples_to_add)
  989. {
  990. if (stream->substream && (stream->status == PCXHR_STREAM_STATUS_RUNNING)) {
  991. u_int64_t new_sample_count;
  992. int elapsed = 0;
  993. int hardware_read = 0;
  994. struct snd_pcm_runtime *runtime = stream->substream->runtime;
  995. if (samples_to_add < 0) {
  996. stream->timer_is_synced = 0;
  997. /* add default if no hardware_read possible */
  998. samples_to_add = PCXHR_GRANULARITY;
  999. }
  1000. if (!stream->timer_is_synced) {
  1001. if (stream->timer_abs_periods != 0 ||
  1002. stream->timer_period_frag + PCXHR_GRANULARITY >=
  1003. runtime->period_size) {
  1004. new_sample_count = pcxhr_stream_read_position(mgr, stream);
  1005. hardware_read = 1;
  1006. if (new_sample_count >= PCXHR_GRANULARITY_MIN) {
  1007. /* sub security offset because of jitter and
  1008. * finer granularity of dsp time (MBOX4)
  1009. */
  1010. new_sample_count -= PCXHR_GRANULARITY_MIN;
  1011. stream->timer_is_synced = 1;
  1012. }
  1013. }
  1014. }
  1015. if (!hardware_read) {
  1016. /* if we didn't try to sync the position, increment it
  1017. * by PCXHR_GRANULARITY every timer interrupt
  1018. */
  1019. new_sample_count = stream->timer_abs_periods +
  1020. stream->timer_period_frag + samples_to_add;
  1021. }
  1022. while (1) {
  1023. u_int64_t new_elapse_pos = stream->timer_abs_periods +
  1024. runtime->period_size;
  1025. if (new_elapse_pos > new_sample_count)
  1026. break;
  1027. elapsed = 1;
  1028. stream->timer_buf_periods++;
  1029. if (stream->timer_buf_periods >= runtime->periods)
  1030. stream->timer_buf_periods = 0;
  1031. stream->timer_abs_periods = new_elapse_pos;
  1032. }
  1033. if (new_sample_count >= stream->timer_abs_periods)
  1034. stream->timer_period_frag = (u_int32_t)(new_sample_count -
  1035. stream->timer_abs_periods);
  1036. else
  1037. snd_printk(KERN_ERR "ERROR new_sample_count too small ??? %lx\n",
  1038. (long unsigned int)new_sample_count);
  1039. if (elapsed) {
  1040. spin_unlock(&mgr->lock);
  1041. snd_pcm_period_elapsed(stream->substream);
  1042. spin_lock(&mgr->lock);
  1043. }
  1044. }
  1045. }
  1046. irqreturn_t pcxhr_interrupt(int irq, void *dev_id)
  1047. {
  1048. struct pcxhr_mgr *mgr = dev_id;
  1049. unsigned int reg;
  1050. int i, j;
  1051. struct snd_pcxhr *chip;
  1052. spin_lock(&mgr->lock);
  1053. reg = PCXHR_INPL(mgr, PCXHR_PLX_IRQCS);
  1054. if (! (reg & PCXHR_IRQCS_ACTIVE_PCIDB)) {
  1055. spin_unlock(&mgr->lock);
  1056. return IRQ_NONE; /* this device did not cause the interrupt */
  1057. }
  1058. /* clear interrupt */
  1059. reg = PCXHR_INPL(mgr, PCXHR_PLX_L2PCIDB);
  1060. PCXHR_OUTPL(mgr, PCXHR_PLX_L2PCIDB, reg);
  1061. /* timer irq occured */
  1062. if (reg & PCXHR_IRQ_TIMER) {
  1063. int timer_toggle = reg & PCXHR_IRQ_TIMER;
  1064. /* is a 24 bit counter */
  1065. int dsp_time_new = PCXHR_INPL(mgr, PCXHR_PLX_MBOX4) & PCXHR_DSP_TIME_MASK;
  1066. int dsp_time_diff = dsp_time_new - mgr->dsp_time_last;
  1067. if (dsp_time_diff < 0 && mgr->dsp_time_last != PCXHR_DSP_TIME_INVALID) {
  1068. snd_printdd("ERROR DSP TIME old(%d) new(%d) -> "
  1069. "resynchronize all streams\n",
  1070. mgr->dsp_time_last, dsp_time_new);
  1071. mgr->dsp_time_err++;
  1072. }
  1073. #ifdef CONFIG_SND_DEBUG_DETECT
  1074. if (dsp_time_diff == 0)
  1075. snd_printdd("ERROR DSP TIME NO DIFF time(%d)\n", dsp_time_new);
  1076. else if (dsp_time_diff >= (2*PCXHR_GRANULARITY))
  1077. snd_printdd("ERROR DSP TIME TOO BIG old(%d) add(%d)\n",
  1078. mgr->dsp_time_last, dsp_time_new - mgr->dsp_time_last);
  1079. #endif
  1080. mgr->dsp_time_last = dsp_time_new;
  1081. if (timer_toggle == mgr->timer_toggle)
  1082. snd_printdd("ERROR TIMER TOGGLE\n");
  1083. mgr->timer_toggle = timer_toggle;
  1084. reg &= ~PCXHR_IRQ_TIMER;
  1085. for (i = 0; i < mgr->num_cards; i++) {
  1086. chip = mgr->chip[i];
  1087. for (j = 0; j < chip->nb_streams_capt; j++)
  1088. pcxhr_update_timer_pos(mgr, &chip->capture_stream[j],
  1089. dsp_time_diff);
  1090. }
  1091. for (i = 0; i < mgr->num_cards; i++) {
  1092. chip = mgr->chip[i];
  1093. for (j = 0; j < chip->nb_streams_play; j++)
  1094. pcxhr_update_timer_pos(mgr, &chip->playback_stream[j],
  1095. dsp_time_diff);
  1096. }
  1097. }
  1098. /* other irq's handled in the tasklet */
  1099. if (reg & PCXHR_IRQ_MASK) {
  1100. /* as we didn't request any notifications, some kind of xrun error
  1101. * will probably occured
  1102. */
  1103. /* better resynchronize all streams next interrupt : */
  1104. mgr->dsp_time_last = PCXHR_DSP_TIME_INVALID;
  1105. mgr->src_it_dsp = reg;
  1106. tasklet_hi_schedule(&mgr->msg_taskq);
  1107. }
  1108. #ifdef CONFIG_SND_DEBUG_DETECT
  1109. if (reg & PCXHR_FATAL_DSP_ERR)
  1110. snd_printdd("FATAL DSP ERROR : %x\n", reg);
  1111. #endif
  1112. spin_unlock(&mgr->lock);
  1113. return IRQ_HANDLED; /* this device caused the interrupt */
  1114. }