pcxhr_core.c 37 KB

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