lx_core.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /* -*- linux-c -*- *
  2. *
  3. * ALSA driver for the digigram lx6464es interface
  4. * low-level interface
  5. *
  6. * Copyright (c) 2009 Tim Blechmann <tim@klingt.org>
  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; see the file COPYING. If not, write to
  20. * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. /* #define RMH_DEBUG 1 */
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <linux/delay.h>
  28. #include "lx6464es.h"
  29. #include "lx_core.h"
  30. /* low-level register access */
  31. static const unsigned long dsp_port_offsets[] = {
  32. 0,
  33. 0x400,
  34. 0x401,
  35. 0x402,
  36. 0x403,
  37. 0x404,
  38. 0x405,
  39. 0x406,
  40. 0x407,
  41. 0x408,
  42. 0x409,
  43. 0x40a,
  44. 0x40b,
  45. 0x40c,
  46. 0x410,
  47. 0x411,
  48. 0x412,
  49. 0x413,
  50. 0x414,
  51. 0x415,
  52. 0x416,
  53. 0x420,
  54. 0x430,
  55. 0x431,
  56. 0x432,
  57. 0x433,
  58. 0x434,
  59. 0x440
  60. };
  61. static void __iomem *lx_dsp_register(struct lx6464es *chip, int port)
  62. {
  63. void __iomem *base_address = chip->port_dsp_bar;
  64. return base_address + dsp_port_offsets[port]*4;
  65. }
  66. unsigned long lx_dsp_reg_read(struct lx6464es *chip, int port)
  67. {
  68. void __iomem *address = lx_dsp_register(chip, port);
  69. return ioread32(address);
  70. }
  71. void lx_dsp_reg_readbuf(struct lx6464es *chip, int port, u32 *data, u32 len)
  72. {
  73. void __iomem *address = lx_dsp_register(chip, port);
  74. memcpy_fromio(data, address, len*sizeof(u32));
  75. }
  76. void lx_dsp_reg_write(struct lx6464es *chip, int port, unsigned data)
  77. {
  78. void __iomem *address = lx_dsp_register(chip, port);
  79. iowrite32(data, address);
  80. }
  81. void lx_dsp_reg_writebuf(struct lx6464es *chip, int port, const u32 *data,
  82. u32 len)
  83. {
  84. void __iomem *address = lx_dsp_register(chip, port);
  85. memcpy_toio(address, data, len*sizeof(u32));
  86. }
  87. static const unsigned long plx_port_offsets[] = {
  88. 0x04,
  89. 0x40,
  90. 0x44,
  91. 0x48,
  92. 0x4c,
  93. 0x50,
  94. 0x54,
  95. 0x58,
  96. 0x5c,
  97. 0x64,
  98. 0x68,
  99. 0x6C
  100. };
  101. static void __iomem *lx_plx_register(struct lx6464es *chip, int port)
  102. {
  103. void __iomem *base_address = chip->port_plx_remapped;
  104. return base_address + plx_port_offsets[port];
  105. }
  106. unsigned long lx_plx_reg_read(struct lx6464es *chip, int port)
  107. {
  108. void __iomem *address = lx_plx_register(chip, port);
  109. return ioread32(address);
  110. }
  111. void lx_plx_reg_write(struct lx6464es *chip, int port, u32 data)
  112. {
  113. void __iomem *address = lx_plx_register(chip, port);
  114. iowrite32(data, address);
  115. }
  116. u32 lx_plx_mbox_read(struct lx6464es *chip, int mbox_nr)
  117. {
  118. int index;
  119. switch (mbox_nr) {
  120. case 1:
  121. index = ePLX_MBOX1; break;
  122. case 2:
  123. index = ePLX_MBOX2; break;
  124. case 3:
  125. index = ePLX_MBOX3; break;
  126. case 4:
  127. index = ePLX_MBOX4; break;
  128. case 5:
  129. index = ePLX_MBOX5; break;
  130. case 6:
  131. index = ePLX_MBOX6; break;
  132. case 7:
  133. index = ePLX_MBOX7; break;
  134. case 0: /* reserved for HF flags */
  135. snd_BUG();
  136. default:
  137. return 0xdeadbeef;
  138. }
  139. return lx_plx_reg_read(chip, index);
  140. }
  141. int lx_plx_mbox_write(struct lx6464es *chip, int mbox_nr, u32 value)
  142. {
  143. int index = -1;
  144. switch (mbox_nr) {
  145. case 1:
  146. index = ePLX_MBOX1; break;
  147. case 3:
  148. index = ePLX_MBOX3; break;
  149. case 4:
  150. index = ePLX_MBOX4; break;
  151. case 5:
  152. index = ePLX_MBOX5; break;
  153. case 6:
  154. index = ePLX_MBOX6; break;
  155. case 7:
  156. index = ePLX_MBOX7; break;
  157. case 0: /* reserved for HF flags */
  158. case 2: /* reserved for Pipe States
  159. * the DSP keeps an image of it */
  160. snd_BUG();
  161. return -EBADRQC;
  162. }
  163. lx_plx_reg_write(chip, index, value);
  164. return 0;
  165. }
  166. /* rmh */
  167. #ifdef CONFIG_SND_DEBUG
  168. #define CMD_NAME(a) a
  169. #else
  170. #define CMD_NAME(a) NULL
  171. #endif
  172. #define Reg_CSM_MR 0x00000002
  173. #define Reg_CSM_MC 0x00000001
  174. struct dsp_cmd_info {
  175. u32 dcCodeOp; /* Op Code of the command (usually 1st 24-bits
  176. * word).*/
  177. u16 dcCmdLength; /* Command length in words of 24 bits.*/
  178. u16 dcStatusType; /* Status type: 0 for fixed length, 1 for
  179. * random. */
  180. u16 dcStatusLength; /* Status length (if fixed).*/
  181. char *dcOpName;
  182. };
  183. /*
  184. Initialization and control data for the Microblaze interface
  185. - OpCode:
  186. the opcode field of the command set at the proper offset
  187. - CmdLength
  188. the number of command words
  189. - StatusType
  190. offset in the status registers: 0 means that the return value may be
  191. different from 0, and must be read
  192. - StatusLength
  193. the number of status words (in addition to the return value)
  194. */
  195. static struct dsp_cmd_info dsp_commands[] =
  196. {
  197. { (CMD_00_INFO_DEBUG << OPCODE_OFFSET) , 1 /*custom*/
  198. , 1 , 0 /**/ , CMD_NAME("INFO_DEBUG") },
  199. { (CMD_01_GET_SYS_CFG << OPCODE_OFFSET) , 1 /**/
  200. , 1 , 2 /**/ , CMD_NAME("GET_SYS_CFG") },
  201. { (CMD_02_SET_GRANULARITY << OPCODE_OFFSET) , 1 /**/
  202. , 1 , 0 /**/ , CMD_NAME("SET_GRANULARITY") },
  203. { (CMD_03_SET_TIMER_IRQ << OPCODE_OFFSET) , 1 /**/
  204. , 1 , 0 /**/ , CMD_NAME("SET_TIMER_IRQ") },
  205. { (CMD_04_GET_EVENT << OPCODE_OFFSET) , 1 /**/
  206. , 1 , 0 /*up to 10*/ , CMD_NAME("GET_EVENT") },
  207. { (CMD_05_GET_PIPES << OPCODE_OFFSET) , 1 /**/
  208. , 1 , 2 /*up to 4*/ , CMD_NAME("GET_PIPES") },
  209. { (CMD_06_ALLOCATE_PIPE << OPCODE_OFFSET) , 1 /**/
  210. , 0 , 0 /**/ , CMD_NAME("ALLOCATE_PIPE") },
  211. { (CMD_07_RELEASE_PIPE << OPCODE_OFFSET) , 1 /**/
  212. , 0 , 0 /**/ , CMD_NAME("RELEASE_PIPE") },
  213. { (CMD_08_ASK_BUFFERS << OPCODE_OFFSET) , 1 /**/
  214. , 1 , MAX_STREAM_BUFFER , CMD_NAME("ASK_BUFFERS") },
  215. { (CMD_09_STOP_PIPE << OPCODE_OFFSET) , 1 /**/
  216. , 0 , 0 /*up to 2*/ , CMD_NAME("STOP_PIPE") },
  217. { (CMD_0A_GET_PIPE_SPL_COUNT << OPCODE_OFFSET) , 1 /**/
  218. , 1 , 1 /*up to 2*/ , CMD_NAME("GET_PIPE_SPL_COUNT") },
  219. { (CMD_0B_TOGGLE_PIPE_STATE << OPCODE_OFFSET) , 1 /*up to 5*/
  220. , 1 , 0 /**/ , CMD_NAME("TOGGLE_PIPE_STATE") },
  221. { (CMD_0C_DEF_STREAM << OPCODE_OFFSET) , 1 /*up to 4*/
  222. , 1 , 0 /**/ , CMD_NAME("DEF_STREAM") },
  223. { (CMD_0D_SET_MUTE << OPCODE_OFFSET) , 3 /**/
  224. , 1 , 0 /**/ , CMD_NAME("SET_MUTE") },
  225. { (CMD_0E_GET_STREAM_SPL_COUNT << OPCODE_OFFSET) , 1/**/
  226. , 1 , 2 /**/ , CMD_NAME("GET_STREAM_SPL_COUNT") },
  227. { (CMD_0F_UPDATE_BUFFER << OPCODE_OFFSET) , 3 /*up to 4*/
  228. , 0 , 1 /**/ , CMD_NAME("UPDATE_BUFFER") },
  229. { (CMD_10_GET_BUFFER << OPCODE_OFFSET) , 1 /**/
  230. , 1 , 4 /**/ , CMD_NAME("GET_BUFFER") },
  231. { (CMD_11_CANCEL_BUFFER << OPCODE_OFFSET) , 1 /**/
  232. , 1 , 1 /*up to 4*/ , CMD_NAME("CANCEL_BUFFER") },
  233. { (CMD_12_GET_PEAK << OPCODE_OFFSET) , 1 /**/
  234. , 1 , 1 /**/ , CMD_NAME("GET_PEAK") },
  235. { (CMD_13_SET_STREAM_STATE << OPCODE_OFFSET) , 1 /**/
  236. , 1 , 0 /**/ , CMD_NAME("SET_STREAM_STATE") },
  237. };
  238. static void lx_message_init(struct lx_rmh *rmh, enum cmd_mb_opcodes cmd)
  239. {
  240. snd_BUG_ON(cmd >= CMD_14_INVALID);
  241. rmh->cmd[0] = dsp_commands[cmd].dcCodeOp;
  242. rmh->cmd_len = dsp_commands[cmd].dcCmdLength;
  243. rmh->stat_len = dsp_commands[cmd].dcStatusLength;
  244. rmh->dsp_stat = dsp_commands[cmd].dcStatusType;
  245. rmh->cmd_idx = cmd;
  246. memset(&rmh->cmd[1], 0, (REG_CRM_NUMBER - 1) * sizeof(u32));
  247. #ifdef CONFIG_SND_DEBUG
  248. memset(rmh->stat, 0, REG_CRM_NUMBER * sizeof(u32));
  249. #endif
  250. #ifdef RMH_DEBUG
  251. rmh->cmd_idx = cmd;
  252. #endif
  253. }
  254. #ifdef RMH_DEBUG
  255. #define LXRMH "lx6464es rmh: "
  256. static void lx_message_dump(struct lx_rmh *rmh)
  257. {
  258. u8 idx = rmh->cmd_idx;
  259. int i;
  260. snd_printk(LXRMH "command %s\n", dsp_commands[idx].dcOpName);
  261. for (i = 0; i != rmh->cmd_len; ++i)
  262. snd_printk(LXRMH "\tcmd[%d] %08x\n", i, rmh->cmd[i]);
  263. for (i = 0; i != rmh->stat_len; ++i)
  264. snd_printk(LXRMH "\tstat[%d]: %08x\n", i, rmh->stat[i]);
  265. snd_printk("\n");
  266. }
  267. #else
  268. static inline void lx_message_dump(struct lx_rmh *rmh)
  269. {}
  270. #endif
  271. /* sleep 500 - 100 = 400 times 100us -> the timeout is >= 40 ms */
  272. #define XILINX_TIMEOUT_MS 40
  273. #define XILINX_POLL_NO_SLEEP 100
  274. #define XILINX_POLL_ITERATIONS 150
  275. #if 0 /* not used now */
  276. static int lx_message_send(struct lx6464es *chip, struct lx_rmh *rmh)
  277. {
  278. u32 reg = ED_DSP_TIMED_OUT;
  279. int dwloop;
  280. int answer_received;
  281. if (lx_dsp_reg_read(chip, eReg_CSM) & (Reg_CSM_MC | Reg_CSM_MR)) {
  282. snd_printk(KERN_ERR LXP "PIOSendMessage eReg_CSM %x\n", reg);
  283. return -EBUSY;
  284. }
  285. /* write command */
  286. lx_dsp_reg_writebuf(chip, eReg_CRM1, rmh->cmd, rmh->cmd_len);
  287. snd_BUG_ON(atomic_read(&chip->send_message_locked) != 0);
  288. atomic_set(&chip->send_message_locked, 1);
  289. /* MicoBlaze gogogo */
  290. lx_dsp_reg_write(chip, eReg_CSM, Reg_CSM_MC);
  291. /* wait for interrupt to answer */
  292. for (dwloop = 0; dwloop != XILINX_TIMEOUT_MS; ++dwloop) {
  293. answer_received = atomic_read(&chip->send_message_locked);
  294. if (answer_received == 0)
  295. break;
  296. msleep(1);
  297. }
  298. if (answer_received == 0) {
  299. /* in Debug mode verify Reg_CSM_MR */
  300. snd_BUG_ON(!(lx_dsp_reg_read(chip, eReg_CSM) & Reg_CSM_MR));
  301. /* command finished, read status */
  302. if (rmh->dsp_stat == 0)
  303. reg = lx_dsp_reg_read(chip, eReg_CRM1);
  304. else
  305. reg = 0;
  306. } else {
  307. int i;
  308. snd_printk(KERN_WARNING LXP "TIMEOUT lx_message_send! "
  309. "Interrupts disabled?\n");
  310. /* attente bit Reg_CSM_MR */
  311. for (i = 0; i != XILINX_POLL_ITERATIONS; i++) {
  312. if ((lx_dsp_reg_read(chip, eReg_CSM) & Reg_CSM_MR)) {
  313. if (rmh->dsp_stat == 0)
  314. reg = lx_dsp_reg_read(chip, eReg_CRM1);
  315. else
  316. reg = 0;
  317. goto polling_successful;
  318. }
  319. if (i > XILINX_POLL_NO_SLEEP)
  320. msleep(1);
  321. }
  322. snd_printk(KERN_WARNING LXP "TIMEOUT lx_message_send! "
  323. "polling failed\n");
  324. polling_successful:
  325. atomic_set(&chip->send_message_locked, 0);
  326. }
  327. if ((reg & ERROR_VALUE) == 0) {
  328. /* read response */
  329. if (rmh->stat_len) {
  330. snd_BUG_ON(rmh->stat_len >= (REG_CRM_NUMBER-1));
  331. lx_dsp_reg_readbuf(chip, eReg_CRM2, rmh->stat,
  332. rmh->stat_len);
  333. }
  334. } else
  335. snd_printk(KERN_WARNING LXP "lx_message_send: error_value %x\n",
  336. reg);
  337. /* clear Reg_CSM_MR */
  338. lx_dsp_reg_write(chip, eReg_CSM, 0);
  339. switch (reg) {
  340. case ED_DSP_TIMED_OUT:
  341. snd_printk(KERN_WARNING LXP "lx_message_send: dsp timeout\n");
  342. return -ETIMEDOUT;
  343. case ED_DSP_CRASHED:
  344. snd_printk(KERN_WARNING LXP "lx_message_send: dsp crashed\n");
  345. return -EAGAIN;
  346. }
  347. lx_message_dump(rmh);
  348. return 0;
  349. }
  350. #endif /* not used now */
  351. static int lx_message_send_atomic(struct lx6464es *chip, struct lx_rmh *rmh)
  352. {
  353. u32 reg = ED_DSP_TIMED_OUT;
  354. int dwloop;
  355. if (lx_dsp_reg_read(chip, eReg_CSM) & (Reg_CSM_MC | Reg_CSM_MR)) {
  356. snd_printk(KERN_ERR LXP "PIOSendMessage eReg_CSM %x\n", reg);
  357. return -EBUSY;
  358. }
  359. /* write command */
  360. lx_dsp_reg_writebuf(chip, eReg_CRM1, rmh->cmd, rmh->cmd_len);
  361. /* MicoBlaze gogogo */
  362. lx_dsp_reg_write(chip, eReg_CSM, Reg_CSM_MC);
  363. /* wait for interrupt to answer */
  364. for (dwloop = 0; dwloop != XILINX_TIMEOUT_MS * 1000; ++dwloop) {
  365. if (lx_dsp_reg_read(chip, eReg_CSM) & Reg_CSM_MR) {
  366. if (rmh->dsp_stat == 0)
  367. reg = lx_dsp_reg_read(chip, eReg_CRM1);
  368. else
  369. reg = 0;
  370. goto polling_successful;
  371. } else
  372. udelay(1);
  373. }
  374. snd_printk(KERN_WARNING LXP "TIMEOUT lx_message_send_atomic! "
  375. "polling failed\n");
  376. polling_successful:
  377. if ((reg & ERROR_VALUE) == 0) {
  378. /* read response */
  379. if (rmh->stat_len) {
  380. snd_BUG_ON(rmh->stat_len >= (REG_CRM_NUMBER-1));
  381. lx_dsp_reg_readbuf(chip, eReg_CRM2, rmh->stat,
  382. rmh->stat_len);
  383. }
  384. } else
  385. snd_printk(LXP "rmh error: %08x\n", reg);
  386. /* clear Reg_CSM_MR */
  387. lx_dsp_reg_write(chip, eReg_CSM, 0);
  388. switch (reg) {
  389. case ED_DSP_TIMED_OUT:
  390. snd_printk(KERN_WARNING LXP "lx_message_send: dsp timeout\n");
  391. return -ETIMEDOUT;
  392. case ED_DSP_CRASHED:
  393. snd_printk(KERN_WARNING LXP "lx_message_send: dsp crashed\n");
  394. return -EAGAIN;
  395. }
  396. lx_message_dump(rmh);
  397. return reg;
  398. }
  399. /* low-level dsp access */
  400. int __devinit lx_dsp_get_version(struct lx6464es *chip, u32 *rdsp_version)
  401. {
  402. u16 ret;
  403. unsigned long flags;
  404. spin_lock_irqsave(&chip->msg_lock, flags);
  405. lx_message_init(&chip->rmh, CMD_01_GET_SYS_CFG);
  406. ret = lx_message_send_atomic(chip, &chip->rmh);
  407. *rdsp_version = chip->rmh.stat[1];
  408. spin_unlock_irqrestore(&chip->msg_lock, flags);
  409. return ret;
  410. }
  411. int lx_dsp_get_clock_frequency(struct lx6464es *chip, u32 *rfreq)
  412. {
  413. u16 ret = 0;
  414. unsigned long flags;
  415. u32 freq_raw = 0;
  416. u32 freq = 0;
  417. u32 frequency = 0;
  418. spin_lock_irqsave(&chip->msg_lock, flags);
  419. lx_message_init(&chip->rmh, CMD_01_GET_SYS_CFG);
  420. ret = lx_message_send_atomic(chip, &chip->rmh);
  421. if (ret == 0) {
  422. freq_raw = chip->rmh.stat[0] >> FREQ_FIELD_OFFSET;
  423. freq = freq_raw & XES_FREQ_COUNT8_MASK;
  424. if ((freq < XES_FREQ_COUNT8_48_MAX) ||
  425. (freq > XES_FREQ_COUNT8_44_MIN))
  426. frequency = 0; /* unknown */
  427. else if (freq >= XES_FREQ_COUNT8_44_MAX)
  428. frequency = 44100;
  429. else
  430. frequency = 48000;
  431. }
  432. spin_unlock_irqrestore(&chip->msg_lock, flags);
  433. *rfreq = frequency * chip->freq_ratio;
  434. return ret;
  435. }
  436. int lx_dsp_get_mac(struct lx6464es *chip, u8 *mac_address)
  437. {
  438. u32 macmsb, maclsb;
  439. macmsb = lx_dsp_reg_read(chip, eReg_ADMACESMSB) & 0x00FFFFFF;
  440. maclsb = lx_dsp_reg_read(chip, eReg_ADMACESLSB) & 0x00FFFFFF;
  441. /* todo: endianess handling */
  442. mac_address[5] = ((u8 *)(&maclsb))[0];
  443. mac_address[4] = ((u8 *)(&maclsb))[1];
  444. mac_address[3] = ((u8 *)(&maclsb))[2];
  445. mac_address[2] = ((u8 *)(&macmsb))[0];
  446. mac_address[1] = ((u8 *)(&macmsb))[1];
  447. mac_address[0] = ((u8 *)(&macmsb))[2];
  448. return 0;
  449. }
  450. int lx_dsp_set_granularity(struct lx6464es *chip, u32 gran)
  451. {
  452. unsigned long flags;
  453. int ret;
  454. spin_lock_irqsave(&chip->msg_lock, flags);
  455. lx_message_init(&chip->rmh, CMD_02_SET_GRANULARITY);
  456. chip->rmh.cmd[0] |= gran;
  457. ret = lx_message_send_atomic(chip, &chip->rmh);
  458. spin_unlock_irqrestore(&chip->msg_lock, flags);
  459. return ret;
  460. }
  461. int lx_dsp_read_async_events(struct lx6464es *chip, u32 *data)
  462. {
  463. unsigned long flags;
  464. int ret;
  465. spin_lock_irqsave(&chip->msg_lock, flags);
  466. lx_message_init(&chip->rmh, CMD_04_GET_EVENT);
  467. chip->rmh.stat_len = 9; /* we don't necessarily need the full length */
  468. ret = lx_message_send_atomic(chip, &chip->rmh);
  469. if (!ret)
  470. memcpy(data, chip->rmh.stat, chip->rmh.stat_len * sizeof(u32));
  471. spin_unlock_irqrestore(&chip->msg_lock, flags);
  472. return ret;
  473. }
  474. #define CSES_TIMEOUT 100 /* microseconds */
  475. #define CSES_CE 0x0001
  476. #define CSES_BROADCAST 0x0002
  477. #define CSES_UPDATE_LDSV 0x0004
  478. int lx_dsp_es_check_pipeline(struct lx6464es *chip)
  479. {
  480. int i;
  481. for (i = 0; i != CSES_TIMEOUT; ++i) {
  482. /*
  483. * le bit CSES_UPDATE_LDSV est à 1 dés que le macprog
  484. * est pret. il re-passe à 0 lorsque le premier read a
  485. * été fait. pour l'instant on retire le test car ce bit
  486. * passe a 1 environ 200 à 400 ms aprés que le registre
  487. * confES à été écrit (kick du xilinx ES).
  488. *
  489. * On ne teste que le bit CE.
  490. * */
  491. u32 cses = lx_dsp_reg_read(chip, eReg_CSES);
  492. if ((cses & CSES_CE) == 0)
  493. return 0;
  494. udelay(1);
  495. }
  496. return -ETIMEDOUT;
  497. }
  498. #define PIPE_INFO_TO_CMD(capture, pipe) \
  499. ((u32)((u32)(pipe) | ((capture) ? ID_IS_CAPTURE : 0L)) << ID_OFFSET)
  500. /* low-level pipe handling */
  501. int lx_pipe_allocate(struct lx6464es *chip, u32 pipe, int is_capture,
  502. int channels)
  503. {
  504. int err;
  505. unsigned long flags;
  506. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  507. spin_lock_irqsave(&chip->msg_lock, flags);
  508. lx_message_init(&chip->rmh, CMD_06_ALLOCATE_PIPE);
  509. chip->rmh.cmd[0] |= pipe_cmd;
  510. chip->rmh.cmd[0] |= channels;
  511. err = lx_message_send_atomic(chip, &chip->rmh);
  512. spin_unlock_irqrestore(&chip->msg_lock, flags);
  513. if (err != 0)
  514. snd_printk(KERN_ERR "lx6464es: could not allocate pipe\n");
  515. return err;
  516. }
  517. int lx_pipe_release(struct lx6464es *chip, u32 pipe, int is_capture)
  518. {
  519. int err;
  520. unsigned long flags;
  521. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  522. spin_lock_irqsave(&chip->msg_lock, flags);
  523. lx_message_init(&chip->rmh, CMD_07_RELEASE_PIPE);
  524. chip->rmh.cmd[0] |= pipe_cmd;
  525. err = lx_message_send_atomic(chip, &chip->rmh);
  526. spin_unlock_irqrestore(&chip->msg_lock, flags);
  527. return err;
  528. }
  529. int lx_buffer_ask(struct lx6464es *chip, u32 pipe, int is_capture,
  530. u32 *r_needed, u32 *r_freed, u32 *size_array)
  531. {
  532. int err;
  533. unsigned long flags;
  534. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  535. #ifdef CONFIG_SND_DEBUG
  536. if (size_array)
  537. memset(size_array, 0, sizeof(u32)*MAX_STREAM_BUFFER);
  538. #endif
  539. *r_needed = 0;
  540. *r_freed = 0;
  541. spin_lock_irqsave(&chip->msg_lock, flags);
  542. lx_message_init(&chip->rmh, CMD_08_ASK_BUFFERS);
  543. chip->rmh.cmd[0] |= pipe_cmd;
  544. err = lx_message_send_atomic(chip, &chip->rmh);
  545. if (!err) {
  546. int i;
  547. for (i = 0; i < MAX_STREAM_BUFFER; ++i) {
  548. u32 stat = chip->rmh.stat[i];
  549. if (stat & (BF_EOB << BUFF_FLAGS_OFFSET)) {
  550. /* finished */
  551. *r_freed += 1;
  552. if (size_array)
  553. size_array[i] = stat & MASK_DATA_SIZE;
  554. } else if ((stat & (BF_VALID << BUFF_FLAGS_OFFSET))
  555. == 0)
  556. /* free */
  557. *r_needed += 1;
  558. }
  559. #if 0
  560. snd_printdd(LXP "CMD_08_ASK_BUFFERS: needed %d, freed %d\n",
  561. *r_needed, *r_freed);
  562. for (i = 0; i < MAX_STREAM_BUFFER; ++i) {
  563. for (i = 0; i != chip->rmh.stat_len; ++i)
  564. snd_printdd(" stat[%d]: %x, %x\n", i,
  565. chip->rmh.stat[i],
  566. chip->rmh.stat[i] & MASK_DATA_SIZE);
  567. }
  568. #endif
  569. }
  570. spin_unlock_irqrestore(&chip->msg_lock, flags);
  571. return err;
  572. }
  573. int lx_pipe_stop(struct lx6464es *chip, u32 pipe, int is_capture)
  574. {
  575. int err;
  576. unsigned long flags;
  577. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  578. spin_lock_irqsave(&chip->msg_lock, flags);
  579. lx_message_init(&chip->rmh, CMD_09_STOP_PIPE);
  580. chip->rmh.cmd[0] |= pipe_cmd;
  581. err = lx_message_send_atomic(chip, &chip->rmh);
  582. spin_unlock_irqrestore(&chip->msg_lock, flags);
  583. return err;
  584. }
  585. static int lx_pipe_toggle_state(struct lx6464es *chip, u32 pipe, int is_capture)
  586. {
  587. int err;
  588. unsigned long flags;
  589. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  590. spin_lock_irqsave(&chip->msg_lock, flags);
  591. lx_message_init(&chip->rmh, CMD_0B_TOGGLE_PIPE_STATE);
  592. chip->rmh.cmd[0] |= pipe_cmd;
  593. err = lx_message_send_atomic(chip, &chip->rmh);
  594. spin_unlock_irqrestore(&chip->msg_lock, flags);
  595. return err;
  596. }
  597. int lx_pipe_start(struct lx6464es *chip, u32 pipe, int is_capture)
  598. {
  599. int err;
  600. err = lx_pipe_wait_for_idle(chip, pipe, is_capture);
  601. if (err < 0)
  602. return err;
  603. err = lx_pipe_toggle_state(chip, pipe, is_capture);
  604. return err;
  605. }
  606. int lx_pipe_pause(struct lx6464es *chip, u32 pipe, int is_capture)
  607. {
  608. int err = 0;
  609. err = lx_pipe_wait_for_start(chip, pipe, is_capture);
  610. if (err < 0)
  611. return err;
  612. err = lx_pipe_toggle_state(chip, pipe, is_capture);
  613. return err;
  614. }
  615. int lx_pipe_sample_count(struct lx6464es *chip, u32 pipe, int is_capture,
  616. u64 *rsample_count)
  617. {
  618. int err;
  619. unsigned long flags;
  620. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  621. spin_lock_irqsave(&chip->msg_lock, flags);
  622. lx_message_init(&chip->rmh, CMD_0A_GET_PIPE_SPL_COUNT);
  623. chip->rmh.cmd[0] |= pipe_cmd;
  624. chip->rmh.stat_len = 2; /* need all words here! */
  625. err = lx_message_send_atomic(chip, &chip->rmh); /* don't sleep! */
  626. if (err != 0)
  627. snd_printk(KERN_ERR
  628. "lx6464es: could not query pipe's sample count\n");
  629. else {
  630. *rsample_count = ((u64)(chip->rmh.stat[0] & MASK_SPL_COUNT_HI)
  631. << 24) /* hi part */
  632. + chip->rmh.stat[1]; /* lo part */
  633. }
  634. spin_unlock_irqrestore(&chip->msg_lock, flags);
  635. return err;
  636. }
  637. int lx_pipe_state(struct lx6464es *chip, u32 pipe, int is_capture, u16 *rstate)
  638. {
  639. int err;
  640. unsigned long flags;
  641. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  642. spin_lock_irqsave(&chip->msg_lock, flags);
  643. lx_message_init(&chip->rmh, CMD_0A_GET_PIPE_SPL_COUNT);
  644. chip->rmh.cmd[0] |= pipe_cmd;
  645. err = lx_message_send_atomic(chip, &chip->rmh);
  646. if (err != 0)
  647. snd_printk(KERN_ERR "lx6464es: could not query pipe's state\n");
  648. else
  649. *rstate = (chip->rmh.stat[0] >> PSTATE_OFFSET) & 0x0F;
  650. spin_unlock_irqrestore(&chip->msg_lock, flags);
  651. return err;
  652. }
  653. static int lx_pipe_wait_for_state(struct lx6464es *chip, u32 pipe,
  654. int is_capture, u16 state)
  655. {
  656. int i;
  657. /* max 2*PCMOnlyGranularity = 2*1024 at 44100 = < 50 ms:
  658. * timeout 50 ms */
  659. for (i = 0; i != 50; ++i) {
  660. u16 current_state;
  661. int err = lx_pipe_state(chip, pipe, is_capture, &current_state);
  662. if (err < 0)
  663. return err;
  664. if (current_state == state)
  665. return 0;
  666. mdelay(1);
  667. }
  668. return -ETIMEDOUT;
  669. }
  670. int lx_pipe_wait_for_start(struct lx6464es *chip, u32 pipe, int is_capture)
  671. {
  672. return lx_pipe_wait_for_state(chip, pipe, is_capture, PSTATE_RUN);
  673. }
  674. int lx_pipe_wait_for_idle(struct lx6464es *chip, u32 pipe, int is_capture)
  675. {
  676. return lx_pipe_wait_for_state(chip, pipe, is_capture, PSTATE_IDLE);
  677. }
  678. /* low-level stream handling */
  679. int lx_stream_set_state(struct lx6464es *chip, u32 pipe,
  680. int is_capture, enum stream_state_t state)
  681. {
  682. int err;
  683. unsigned long flags;
  684. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  685. spin_lock_irqsave(&chip->msg_lock, flags);
  686. lx_message_init(&chip->rmh, CMD_13_SET_STREAM_STATE);
  687. chip->rmh.cmd[0] |= pipe_cmd;
  688. chip->rmh.cmd[0] |= state;
  689. err = lx_message_send_atomic(chip, &chip->rmh);
  690. spin_unlock_irqrestore(&chip->msg_lock, flags);
  691. return err;
  692. }
  693. int lx_stream_set_format(struct lx6464es *chip, struct snd_pcm_runtime *runtime,
  694. u32 pipe, int is_capture)
  695. {
  696. int err;
  697. unsigned long flags;
  698. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  699. u32 channels = runtime->channels;
  700. if (runtime->channels != channels)
  701. snd_printk(KERN_ERR LXP "channel count mismatch: %d vs %d",
  702. runtime->channels, channels);
  703. spin_lock_irqsave(&chip->msg_lock, flags);
  704. lx_message_init(&chip->rmh, CMD_0C_DEF_STREAM);
  705. chip->rmh.cmd[0] |= pipe_cmd;
  706. if (runtime->sample_bits == 16)
  707. /* 16 bit format */
  708. chip->rmh.cmd[0] |= (STREAM_FMT_16b << STREAM_FMT_OFFSET);
  709. if (snd_pcm_format_little_endian(runtime->format))
  710. /* little endian/intel format */
  711. chip->rmh.cmd[0] |= (STREAM_FMT_intel << STREAM_FMT_OFFSET);
  712. chip->rmh.cmd[0] |= channels-1;
  713. err = lx_message_send_atomic(chip, &chip->rmh);
  714. spin_unlock_irqrestore(&chip->msg_lock, flags);
  715. return err;
  716. }
  717. int lx_stream_state(struct lx6464es *chip, u32 pipe, int is_capture,
  718. int *rstate)
  719. {
  720. int err;
  721. unsigned long flags;
  722. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  723. spin_lock_irqsave(&chip->msg_lock, flags);
  724. lx_message_init(&chip->rmh, CMD_0E_GET_STREAM_SPL_COUNT);
  725. chip->rmh.cmd[0] |= pipe_cmd;
  726. err = lx_message_send_atomic(chip, &chip->rmh);
  727. *rstate = (chip->rmh.stat[0] & SF_START) ? START_STATE : PAUSE_STATE;
  728. spin_unlock_irqrestore(&chip->msg_lock, flags);
  729. return err;
  730. }
  731. int lx_stream_sample_position(struct lx6464es *chip, u32 pipe, int is_capture,
  732. u64 *r_bytepos)
  733. {
  734. int err;
  735. unsigned long flags;
  736. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  737. spin_lock_irqsave(&chip->msg_lock, flags);
  738. lx_message_init(&chip->rmh, CMD_0E_GET_STREAM_SPL_COUNT);
  739. chip->rmh.cmd[0] |= pipe_cmd;
  740. err = lx_message_send_atomic(chip, &chip->rmh);
  741. *r_bytepos = ((u64) (chip->rmh.stat[0] & MASK_SPL_COUNT_HI)
  742. << 32) /* hi part */
  743. + chip->rmh.stat[1]; /* lo part */
  744. spin_unlock_irqrestore(&chip->msg_lock, flags);
  745. return err;
  746. }
  747. /* low-level buffer handling */
  748. int lx_buffer_give(struct lx6464es *chip, u32 pipe, int is_capture,
  749. u32 buffer_size, u32 buf_address_lo, u32 buf_address_hi,
  750. u32 *r_buffer_index)
  751. {
  752. int err;
  753. unsigned long flags;
  754. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  755. spin_lock_irqsave(&chip->msg_lock, flags);
  756. lx_message_init(&chip->rmh, CMD_0F_UPDATE_BUFFER);
  757. chip->rmh.cmd[0] |= pipe_cmd;
  758. chip->rmh.cmd[0] |= BF_NOTIFY_EOB; /* request interrupt notification */
  759. /* todo: pause request, circular buffer */
  760. chip->rmh.cmd[1] = buffer_size & MASK_DATA_SIZE;
  761. chip->rmh.cmd[2] = buf_address_lo;
  762. if (buf_address_hi) {
  763. chip->rmh.cmd_len = 4;
  764. chip->rmh.cmd[3] = buf_address_hi;
  765. chip->rmh.cmd[0] |= BF_64BITS_ADR;
  766. }
  767. err = lx_message_send_atomic(chip, &chip->rmh);
  768. if (err == 0) {
  769. *r_buffer_index = chip->rmh.stat[0];
  770. goto done;
  771. }
  772. if (err == EB_RBUFFERS_TABLE_OVERFLOW)
  773. snd_printk(LXP "lx_buffer_give EB_RBUFFERS_TABLE_OVERFLOW\n");
  774. if (err == EB_INVALID_STREAM)
  775. snd_printk(LXP "lx_buffer_give EB_INVALID_STREAM\n");
  776. if (err == EB_CMD_REFUSED)
  777. snd_printk(LXP "lx_buffer_give EB_CMD_REFUSED\n");
  778. done:
  779. spin_unlock_irqrestore(&chip->msg_lock, flags);
  780. return err;
  781. }
  782. int lx_buffer_free(struct lx6464es *chip, u32 pipe, int is_capture,
  783. u32 *r_buffer_size)
  784. {
  785. int err;
  786. unsigned long flags;
  787. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  788. spin_lock_irqsave(&chip->msg_lock, flags);
  789. lx_message_init(&chip->rmh, CMD_11_CANCEL_BUFFER);
  790. chip->rmh.cmd[0] |= pipe_cmd;
  791. chip->rmh.cmd[0] |= MASK_BUFFER_ID; /* ask for the current buffer: the
  792. * microblaze will seek for it */
  793. err = lx_message_send_atomic(chip, &chip->rmh);
  794. if (err == 0)
  795. *r_buffer_size = chip->rmh.stat[0] & MASK_DATA_SIZE;
  796. spin_unlock_irqrestore(&chip->msg_lock, flags);
  797. return err;
  798. }
  799. int lx_buffer_cancel(struct lx6464es *chip, u32 pipe, int is_capture,
  800. u32 buffer_index)
  801. {
  802. int err;
  803. unsigned long flags;
  804. u32 pipe_cmd = PIPE_INFO_TO_CMD(is_capture, pipe);
  805. spin_lock_irqsave(&chip->msg_lock, flags);
  806. lx_message_init(&chip->rmh, CMD_11_CANCEL_BUFFER);
  807. chip->rmh.cmd[0] |= pipe_cmd;
  808. chip->rmh.cmd[0] |= buffer_index;
  809. err = lx_message_send_atomic(chip, &chip->rmh);
  810. spin_unlock_irqrestore(&chip->msg_lock, flags);
  811. return err;
  812. }
  813. /* low-level gain/peak handling
  814. *
  815. * \todo: can we unmute capture/playback channels independently?
  816. *
  817. * */
  818. int lx_level_unmute(struct lx6464es *chip, int is_capture, int unmute)
  819. {
  820. int err;
  821. unsigned long flags;
  822. /* bit set to 1: channel muted */
  823. u64 mute_mask = unmute ? 0 : 0xFFFFFFFFFFFFFFFFLLU;
  824. spin_lock_irqsave(&chip->msg_lock, flags);
  825. lx_message_init(&chip->rmh, CMD_0D_SET_MUTE);
  826. chip->rmh.cmd[0] |= PIPE_INFO_TO_CMD(is_capture, 0);
  827. chip->rmh.cmd[1] = (u32)(mute_mask >> (u64)32); /* hi part */
  828. chip->rmh.cmd[2] = (u32)(mute_mask & (u64)0xFFFFFFFF); /* lo part */
  829. snd_printk("mute %x %x %x\n", chip->rmh.cmd[0], chip->rmh.cmd[1],
  830. chip->rmh.cmd[2]);
  831. err = lx_message_send_atomic(chip, &chip->rmh);
  832. spin_unlock_irqrestore(&chip->msg_lock, flags);
  833. return err;
  834. }
  835. static u32 peak_map[] = {
  836. 0x00000109, /* -90.308dB */
  837. 0x0000083B, /* -72.247dB */
  838. 0x000020C4, /* -60.205dB */
  839. 0x00008273, /* -48.030dB */
  840. 0x00020756, /* -36.005dB */
  841. 0x00040C37, /* -30.001dB */
  842. 0x00081385, /* -24.002dB */
  843. 0x00101D3F, /* -18.000dB */
  844. 0x0016C310, /* -15.000dB */
  845. 0x002026F2, /* -12.001dB */
  846. 0x002D6A86, /* -9.000dB */
  847. 0x004026E6, /* -6.004dB */
  848. 0x005A9DF6, /* -3.000dB */
  849. 0x0065AC8B, /* -2.000dB */
  850. 0x00721481, /* -1.000dB */
  851. 0x007FFFFF, /* FS */
  852. };
  853. int lx_level_peaks(struct lx6464es *chip, int is_capture, int channels,
  854. u32 *r_levels)
  855. {
  856. int err = 0;
  857. unsigned long flags;
  858. int i;
  859. spin_lock_irqsave(&chip->msg_lock, flags);
  860. for (i = 0; i < channels; i += 4) {
  861. u32 s0, s1, s2, s3;
  862. lx_message_init(&chip->rmh, CMD_12_GET_PEAK);
  863. chip->rmh.cmd[0] |= PIPE_INFO_TO_CMD(is_capture, i);
  864. err = lx_message_send_atomic(chip, &chip->rmh);
  865. if (err == 0) {
  866. s0 = peak_map[chip->rmh.stat[0] & 0x0F];
  867. s1 = peak_map[(chip->rmh.stat[0] >> 4) & 0xf];
  868. s2 = peak_map[(chip->rmh.stat[0] >> 8) & 0xf];
  869. s3 = peak_map[(chip->rmh.stat[0] >> 12) & 0xf];
  870. } else
  871. s0 = s1 = s2 = s3 = 0;
  872. r_levels[0] = s0;
  873. r_levels[1] = s1;
  874. r_levels[2] = s2;
  875. r_levels[3] = s3;
  876. r_levels += 4;
  877. }
  878. spin_unlock_irqrestore(&chip->msg_lock, flags);
  879. return err;
  880. }
  881. /* interrupt handling */
  882. #define PCX_IRQ_NONE 0
  883. #define IRQCS_ACTIVE_PCIDB 0x00002000L /* Bit nø 13 */
  884. #define IRQCS_ENABLE_PCIIRQ 0x00000100L /* Bit nø 08 */
  885. #define IRQCS_ENABLE_PCIDB 0x00000200L /* Bit nø 09 */
  886. static u32 lx_interrupt_test_ack(struct lx6464es *chip)
  887. {
  888. u32 irqcs = lx_plx_reg_read(chip, ePLX_IRQCS);
  889. /* Test if PCI Doorbell interrupt is active */
  890. if (irqcs & IRQCS_ACTIVE_PCIDB) {
  891. u32 temp;
  892. irqcs = PCX_IRQ_NONE;
  893. while ((temp = lx_plx_reg_read(chip, ePLX_L2PCIDB))) {
  894. /* RAZ interrupt */
  895. irqcs |= temp;
  896. lx_plx_reg_write(chip, ePLX_L2PCIDB, temp);
  897. }
  898. return irqcs;
  899. }
  900. return PCX_IRQ_NONE;
  901. }
  902. static int lx_interrupt_ack(struct lx6464es *chip, u32 *r_irqsrc,
  903. int *r_async_pending, int *r_async_escmd)
  904. {
  905. u32 irq_async;
  906. u32 irqsrc = lx_interrupt_test_ack(chip);
  907. if (irqsrc == PCX_IRQ_NONE)
  908. return 0;
  909. *r_irqsrc = irqsrc;
  910. irq_async = irqsrc & MASK_SYS_ASYNC_EVENTS; /* + EtherSound response
  911. * (set by xilinx) + EOB */
  912. if (irq_async & MASK_SYS_STATUS_ESA) {
  913. irq_async &= ~MASK_SYS_STATUS_ESA;
  914. *r_async_escmd = 1;
  915. }
  916. if (irqsrc & MASK_SYS_STATUS_CMD_DONE)
  917. /* xilinx command notification */
  918. atomic_set(&chip->send_message_locked, 0);
  919. if (irq_async) {
  920. /* snd_printd("interrupt: async event pending\n"); */
  921. *r_async_pending = 1;
  922. }
  923. return 1;
  924. }
  925. static int lx_interrupt_handle_async_events(struct lx6464es *chip, u32 irqsrc,
  926. int *r_freq_changed,
  927. u64 *r_notified_in_pipe_mask,
  928. u64 *r_notified_out_pipe_mask)
  929. {
  930. int err;
  931. u32 stat[9]; /* answer from CMD_04_GET_EVENT */
  932. /* On peut optimiser pour ne pas lire les evenements vides
  933. * les mots de réponse sont dans l'ordre suivant :
  934. * Stat[0] mot de status général
  935. * Stat[1] fin de buffer OUT pF
  936. * Stat[2] fin de buffer OUT pf
  937. * Stat[3] fin de buffer IN pF
  938. * Stat[4] fin de buffer IN pf
  939. * Stat[5] underrun poid fort
  940. * Stat[6] underrun poid faible
  941. * Stat[7] overrun poid fort
  942. * Stat[8] overrun poid faible
  943. * */
  944. u64 orun_mask;
  945. u64 urun_mask;
  946. #if 0
  947. int has_underrun = (irqsrc & MASK_SYS_STATUS_URUN) ? 1 : 0;
  948. int has_overrun = (irqsrc & MASK_SYS_STATUS_ORUN) ? 1 : 0;
  949. #endif
  950. int eb_pending_out = (irqsrc & MASK_SYS_STATUS_EOBO) ? 1 : 0;
  951. int eb_pending_in = (irqsrc & MASK_SYS_STATUS_EOBI) ? 1 : 0;
  952. *r_freq_changed = (irqsrc & MASK_SYS_STATUS_FREQ) ? 1 : 0;
  953. err = lx_dsp_read_async_events(chip, stat);
  954. if (err < 0)
  955. return err;
  956. if (eb_pending_in) {
  957. *r_notified_in_pipe_mask = ((u64)stat[3] << 32)
  958. + stat[4];
  959. snd_printdd(LXP "interrupt: EOBI pending %llx\n",
  960. *r_notified_in_pipe_mask);
  961. }
  962. if (eb_pending_out) {
  963. *r_notified_out_pipe_mask = ((u64)stat[1] << 32)
  964. + stat[2];
  965. snd_printdd(LXP "interrupt: EOBO pending %llx\n",
  966. *r_notified_out_pipe_mask);
  967. }
  968. orun_mask = ((u64)stat[7] << 32) + stat[8];
  969. urun_mask = ((u64)stat[5] << 32) + stat[6];
  970. /* todo: handle xrun notification */
  971. return err;
  972. }
  973. static int lx_interrupt_request_new_buffer(struct lx6464es *chip,
  974. struct lx_stream *lx_stream)
  975. {
  976. struct snd_pcm_substream *substream = lx_stream->stream;
  977. int is_capture = lx_stream->is_capture;
  978. int err;
  979. unsigned long flags;
  980. const u32 channels = substream->runtime->channels;
  981. const u32 bytes_per_frame = channels * 3;
  982. const u32 period_size = substream->runtime->period_size;
  983. const u32 period_bytes = period_size * bytes_per_frame;
  984. const u32 pos = lx_stream->frame_pos;
  985. const u32 next_pos = ((pos+1) == substream->runtime->periods) ?
  986. 0 : pos + 1;
  987. dma_addr_t buf = substream->dma_buffer.addr + pos * period_bytes;
  988. u32 buf_hi = 0;
  989. u32 buf_lo = 0;
  990. u32 buffer_index = 0;
  991. u32 needed, freed;
  992. u32 size_array[MAX_STREAM_BUFFER];
  993. snd_printdd("->lx_interrupt_request_new_buffer\n");
  994. spin_lock_irqsave(&chip->lock, flags);
  995. err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed, size_array);
  996. snd_printdd(LXP "interrupt: needed %d, freed %d\n", needed, freed);
  997. unpack_pointer(buf, &buf_lo, &buf_hi);
  998. err = lx_buffer_give(chip, 0, is_capture, period_bytes, buf_lo, buf_hi,
  999. &buffer_index);
  1000. snd_printdd(LXP "interrupt: gave buffer index %x on %p (%d bytes)\n",
  1001. buffer_index, (void *)buf, period_bytes);
  1002. lx_stream->frame_pos = next_pos;
  1003. spin_unlock_irqrestore(&chip->lock, flags);
  1004. return err;
  1005. }
  1006. void lx_tasklet_playback(unsigned long data)
  1007. {
  1008. struct lx6464es *chip = (struct lx6464es *)data;
  1009. struct lx_stream *lx_stream = &chip->playback_stream;
  1010. int err;
  1011. snd_printdd("->lx_tasklet_playback\n");
  1012. err = lx_interrupt_request_new_buffer(chip, lx_stream);
  1013. if (err < 0)
  1014. snd_printk(KERN_ERR LXP
  1015. "cannot request new buffer for playback\n");
  1016. snd_pcm_period_elapsed(lx_stream->stream);
  1017. }
  1018. void lx_tasklet_capture(unsigned long data)
  1019. {
  1020. struct lx6464es *chip = (struct lx6464es *)data;
  1021. struct lx_stream *lx_stream = &chip->capture_stream;
  1022. int err;
  1023. snd_printdd("->lx_tasklet_capture\n");
  1024. err = lx_interrupt_request_new_buffer(chip, lx_stream);
  1025. if (err < 0)
  1026. snd_printk(KERN_ERR LXP
  1027. "cannot request new buffer for capture\n");
  1028. snd_pcm_period_elapsed(lx_stream->stream);
  1029. }
  1030. static int lx_interrupt_handle_audio_transfer(struct lx6464es *chip,
  1031. u64 notified_in_pipe_mask,
  1032. u64 notified_out_pipe_mask)
  1033. {
  1034. int err = 0;
  1035. if (notified_in_pipe_mask) {
  1036. snd_printdd(LXP "requesting audio transfer for capture\n");
  1037. tasklet_hi_schedule(&chip->tasklet_capture);
  1038. }
  1039. if (notified_out_pipe_mask) {
  1040. snd_printdd(LXP "requesting audio transfer for playback\n");
  1041. tasklet_hi_schedule(&chip->tasklet_playback);
  1042. }
  1043. return err;
  1044. }
  1045. irqreturn_t lx_interrupt(int irq, void *dev_id)
  1046. {
  1047. struct lx6464es *chip = dev_id;
  1048. int async_pending, async_escmd;
  1049. u32 irqsrc;
  1050. spin_lock(&chip->lock);
  1051. snd_printdd("**************************************************\n");
  1052. if (!lx_interrupt_ack(chip, &irqsrc, &async_pending, &async_escmd)) {
  1053. spin_unlock(&chip->lock);
  1054. snd_printdd("IRQ_NONE\n");
  1055. return IRQ_NONE; /* this device did not cause the interrupt */
  1056. }
  1057. if (irqsrc & MASK_SYS_STATUS_CMD_DONE)
  1058. goto exit;
  1059. #if 0
  1060. if (irqsrc & MASK_SYS_STATUS_EOBI)
  1061. snd_printdd(LXP "interrupt: EOBI\n");
  1062. if (irqsrc & MASK_SYS_STATUS_EOBO)
  1063. snd_printdd(LXP "interrupt: EOBO\n");
  1064. if (irqsrc & MASK_SYS_STATUS_URUN)
  1065. snd_printdd(LXP "interrupt: URUN\n");
  1066. if (irqsrc & MASK_SYS_STATUS_ORUN)
  1067. snd_printdd(LXP "interrupt: ORUN\n");
  1068. #endif
  1069. if (async_pending) {
  1070. u64 notified_in_pipe_mask = 0;
  1071. u64 notified_out_pipe_mask = 0;
  1072. int freq_changed;
  1073. int err;
  1074. /* handle async events */
  1075. err = lx_interrupt_handle_async_events(chip, irqsrc,
  1076. &freq_changed,
  1077. &notified_in_pipe_mask,
  1078. &notified_out_pipe_mask);
  1079. if (err)
  1080. snd_printk(KERN_ERR LXP
  1081. "error handling async events\n");
  1082. err = lx_interrupt_handle_audio_transfer(chip,
  1083. notified_in_pipe_mask,
  1084. notified_out_pipe_mask
  1085. );
  1086. if (err)
  1087. snd_printk(KERN_ERR LXP
  1088. "error during audio transfer\n");
  1089. }
  1090. if (async_escmd) {
  1091. #if 0
  1092. /* backdoor for ethersound commands
  1093. *
  1094. * for now, we do not need this
  1095. *
  1096. * */
  1097. snd_printdd("lx6464es: interrupt requests escmd handling\n");
  1098. #endif
  1099. }
  1100. exit:
  1101. spin_unlock(&chip->lock);
  1102. return IRQ_HANDLED; /* this device caused the interrupt */
  1103. }
  1104. static void lx_irq_set(struct lx6464es *chip, int enable)
  1105. {
  1106. u32 reg = lx_plx_reg_read(chip, ePLX_IRQCS);
  1107. /* enable/disable interrupts
  1108. *
  1109. * Set the Doorbell and PCI interrupt enable bits
  1110. *
  1111. * */
  1112. if (enable)
  1113. reg |= (IRQCS_ENABLE_PCIIRQ | IRQCS_ENABLE_PCIDB);
  1114. else
  1115. reg &= ~(IRQCS_ENABLE_PCIIRQ | IRQCS_ENABLE_PCIDB);
  1116. lx_plx_reg_write(chip, ePLX_IRQCS, reg);
  1117. }
  1118. void lx_irq_enable(struct lx6464es *chip)
  1119. {
  1120. snd_printdd("->lx_irq_enable\n");
  1121. lx_irq_set(chip, 1);
  1122. }
  1123. void lx_irq_disable(struct lx6464es *chip)
  1124. {
  1125. snd_printdd("->lx_irq_disable\n");
  1126. lx_irq_set(chip, 0);
  1127. }