pcxhr_core.c 37 KB

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