pcxhr_core.c 35 KB

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