aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDylan Reid <dgreid@chromium.org>2014-02-28 18:41:25 -0500
committerTakashi Iwai <tiwai@suse.de>2014-03-01 05:22:46 -0500
commit6e85dddc1c79e8efdc8f670940e98151df91dc08 (patch)
treed3210e759a07015967cc70399b508e7aa3fd72cd
parent2b5fd6c2e9f2398962a932f85d951bce794f97f8 (diff)
ALSA: hda - Relocate RIRB/CORB interface to hda_controller
This is done to allow an HDA platform driver to reuse the code. A few of the interfaces added to hda_controller will disappear in following commits as their users are also moved to hda_controller. Signed-off-by: Dylan Reid <dgreid@chromium.org> Signed-off-by: Takashi Iwai <tiwai@suse.de>
-rw-r--r--sound/pci/hda/hda_controller.c385
-rw-r--r--sound/pci/hda/hda_controller.h11
-rw-r--r--sound/pci/hda/hda_intel.c383
3 files changed, 396 insertions, 383 deletions
diff --git a/sound/pci/hda/hda_controller.c b/sound/pci/hda/hda_controller.c
index b637d2c46237..ed76f8147b58 100644
--- a/sound/pci/hda/hda_controller.c
+++ b/sound/pci/hda/hda_controller.c
@@ -1023,6 +1023,391 @@ int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
1023} 1023}
1024EXPORT_SYMBOL_GPL(azx_attach_pcm_stream); 1024EXPORT_SYMBOL_GPL(azx_attach_pcm_stream);
1025 1025
1026/*
1027 * CORB / RIRB interface
1028 */
1029int azx_alloc_cmd_io(struct azx *chip)
1030{
1031 int err;
1032
1033 /* single page (at least 4096 bytes) must suffice for both ringbuffes */
1034 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV,
1035 PAGE_SIZE, &chip->rb);
1036 if (err < 0)
1037 dev_err(chip->card->dev, "cannot allocate CORB/RIRB\n");
1038 return err;
1039}
1040EXPORT_SYMBOL_GPL(azx_alloc_cmd_io);
1041
1042void azx_init_cmd_io(struct azx *chip)
1043{
1044 int timeout;
1045
1046 spin_lock_irq(&chip->reg_lock);
1047 /* CORB set up */
1048 chip->corb.addr = chip->rb.addr;
1049 chip->corb.buf = (u32 *)chip->rb.area;
1050 azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
1051 azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
1052
1053 /* set the corb size to 256 entries (ULI requires explicitly) */
1054 azx_writeb(chip, CORBSIZE, 0x02);
1055 /* set the corb write pointer to 0 */
1056 azx_writew(chip, CORBWP, 0);
1057
1058 /* reset the corb hw read pointer */
1059 azx_writew(chip, CORBRP, ICH6_CORBRP_RST);
1060 for (timeout = 1000; timeout > 0; timeout--) {
1061 if ((azx_readw(chip, CORBRP) & ICH6_CORBRP_RST) == ICH6_CORBRP_RST)
1062 break;
1063 udelay(1);
1064 }
1065 if (timeout <= 0)
1066 dev_err(chip->card->dev, "CORB reset timeout#1, CORBRP = %d\n",
1067 azx_readw(chip, CORBRP));
1068
1069 azx_writew(chip, CORBRP, 0);
1070 for (timeout = 1000; timeout > 0; timeout--) {
1071 if (azx_readw(chip, CORBRP) == 0)
1072 break;
1073 udelay(1);
1074 }
1075 if (timeout <= 0)
1076 dev_err(chip->card->dev, "CORB reset timeout#2, CORBRP = %d\n",
1077 azx_readw(chip, CORBRP));
1078
1079 /* enable corb dma */
1080 azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN);
1081
1082 /* RIRB set up */
1083 chip->rirb.addr = chip->rb.addr + 2048;
1084 chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
1085 chip->rirb.wp = chip->rirb.rp = 0;
1086 memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
1087 azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
1088 azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
1089
1090 /* set the rirb size to 256 entries (ULI requires explicitly) */
1091 azx_writeb(chip, RIRBSIZE, 0x02);
1092 /* reset the rirb hw write pointer */
1093 azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST);
1094 /* set N=1, get RIRB response interrupt for new entry */
1095 if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
1096 azx_writew(chip, RINTCNT, 0xc0);
1097 else
1098 azx_writew(chip, RINTCNT, 1);
1099 /* enable rirb dma and response irq */
1100 azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
1101 spin_unlock_irq(&chip->reg_lock);
1102}
1103EXPORT_SYMBOL_GPL(azx_init_cmd_io);
1104
1105void azx_free_cmd_io(struct azx *chip)
1106{
1107 spin_lock_irq(&chip->reg_lock);
1108 /* disable ringbuffer DMAs */
1109 azx_writeb(chip, RIRBCTL, 0);
1110 azx_writeb(chip, CORBCTL, 0);
1111 spin_unlock_irq(&chip->reg_lock);
1112}
1113EXPORT_SYMBOL_GPL(azx_free_cmd_io);
1114
1115static unsigned int azx_command_addr(u32 cmd)
1116{
1117 unsigned int addr = cmd >> 28;
1118
1119 if (addr >= AZX_MAX_CODECS) {
1120 snd_BUG();
1121 addr = 0;
1122 }
1123
1124 return addr;
1125}
1126
1127/* send a command */
1128static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
1129{
1130 struct azx *chip = bus->private_data;
1131 unsigned int addr = azx_command_addr(val);
1132 unsigned int wp, rp;
1133
1134 spin_lock_irq(&chip->reg_lock);
1135
1136 /* add command to corb */
1137 wp = azx_readw(chip, CORBWP);
1138 if (wp == 0xffff) {
1139 /* something wrong, controller likely turned to D3 */
1140 spin_unlock_irq(&chip->reg_lock);
1141 return -EIO;
1142 }
1143 wp++;
1144 wp %= ICH6_MAX_CORB_ENTRIES;
1145
1146 rp = azx_readw(chip, CORBRP);
1147 if (wp == rp) {
1148 /* oops, it's full */
1149 spin_unlock_irq(&chip->reg_lock);
1150 return -EAGAIN;
1151 }
1152
1153 chip->rirb.cmds[addr]++;
1154 chip->corb.buf[wp] = cpu_to_le32(val);
1155 azx_writew(chip, CORBWP, wp);
1156
1157 spin_unlock_irq(&chip->reg_lock);
1158
1159 return 0;
1160}
1161
1162#define ICH6_RIRB_EX_UNSOL_EV (1<<4)
1163
1164/* retrieve RIRB entry - called from interrupt handler */
1165void azx_update_rirb(struct azx *chip)
1166{
1167 unsigned int rp, wp;
1168 unsigned int addr;
1169 u32 res, res_ex;
1170
1171 wp = azx_readw(chip, RIRBWP);
1172 if (wp == 0xffff) {
1173 /* something wrong, controller likely turned to D3 */
1174 return;
1175 }
1176
1177 if (wp == chip->rirb.wp)
1178 return;
1179 chip->rirb.wp = wp;
1180
1181 while (chip->rirb.rp != wp) {
1182 chip->rirb.rp++;
1183 chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
1184
1185 rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
1186 res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
1187 res = le32_to_cpu(chip->rirb.buf[rp]);
1188 addr = res_ex & 0xf;
1189 if ((addr >= AZX_MAX_CODECS) || !(chip->codec_mask & (1 << addr))) {
1190 dev_err(chip->card->dev, "spurious response %#x:%#x, rp = %d, wp = %d",
1191 res, res_ex,
1192 chip->rirb.rp, wp);
1193 snd_BUG();
1194 }
1195 else if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
1196 snd_hda_queue_unsol_event(chip->bus, res, res_ex);
1197 else if (chip->rirb.cmds[addr]) {
1198 chip->rirb.res[addr] = res;
1199 smp_wmb();
1200 chip->rirb.cmds[addr]--;
1201 } else if (printk_ratelimit()) {
1202 dev_err(chip->card->dev, "spurious response %#x:%#x, last cmd=%#08x\n",
1203 res, res_ex,
1204 chip->last_cmd[addr]);
1205 }
1206 }
1207}
1208EXPORT_SYMBOL_GPL(azx_update_rirb);
1209
1210/* receive a response */
1211static unsigned int azx_rirb_get_response(struct hda_bus *bus,
1212 unsigned int addr)
1213{
1214 struct azx *chip = bus->private_data;
1215 unsigned long timeout;
1216 unsigned long loopcounter;
1217 int do_poll = 0;
1218
1219 again:
1220 timeout = jiffies + msecs_to_jiffies(1000);
1221
1222 for (loopcounter = 0;; loopcounter++) {
1223 if (chip->polling_mode || do_poll) {
1224 spin_lock_irq(&chip->reg_lock);
1225 azx_update_rirb(chip);
1226 spin_unlock_irq(&chip->reg_lock);
1227 }
1228 if (!chip->rirb.cmds[addr]) {
1229 smp_rmb();
1230 bus->rirb_error = 0;
1231
1232 if (!do_poll)
1233 chip->poll_count = 0;
1234 return chip->rirb.res[addr]; /* the last value */
1235 }
1236 if (time_after(jiffies, timeout))
1237 break;
1238 if (bus->needs_damn_long_delay || loopcounter > 3000)
1239 msleep(2); /* temporary workaround */
1240 else {
1241 udelay(10);
1242 cond_resched();
1243 }
1244 }
1245
1246 if (!bus->no_response_fallback)
1247 return -1;
1248
1249 if (!chip->polling_mode && chip->poll_count < 2) {
1250 dev_dbg(chip->card->dev,
1251 "azx_get_response timeout, polling the codec once: last cmd=0x%08x\n",
1252 chip->last_cmd[addr]);
1253 do_poll = 1;
1254 chip->poll_count++;
1255 goto again;
1256 }
1257
1258
1259 if (!chip->polling_mode) {
1260 dev_warn(chip->card->dev,
1261 "azx_get_response timeout, switching to polling mode: last cmd=0x%08x\n",
1262 chip->last_cmd[addr]);
1263 chip->polling_mode = 1;
1264 goto again;
1265 }
1266
1267 if (chip->msi) {
1268 dev_warn(chip->card->dev,
1269 "No response from codec, disabling MSI: last cmd=0x%08x\n",
1270 chip->last_cmd[addr]);
1271 if (chip->ops->disable_msi_reset_irq(chip) &&
1272 chip->ops->disable_msi_reset_irq(chip) < 0) {
1273 bus->rirb_error = 1;
1274 return -1;
1275 }
1276 goto again;
1277 }
1278
1279 if (chip->probing) {
1280 /* If this critical timeout happens during the codec probing
1281 * phase, this is likely an access to a non-existing codec
1282 * slot. Better to return an error and reset the system.
1283 */
1284 return -1;
1285 }
1286
1287 /* a fatal communication error; need either to reset or to fallback
1288 * to the single_cmd mode
1289 */
1290 bus->rirb_error = 1;
1291 if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
1292 bus->response_reset = 1;
1293 return -1; /* give a chance to retry */
1294 }
1295
1296 dev_err(chip->card->dev,
1297 "azx_get_response timeout, switching to single_cmd mode: last cmd=0x%08x\n",
1298 chip->last_cmd[addr]);
1299 chip->single_cmd = 1;
1300 bus->response_reset = 0;
1301 /* release CORB/RIRB */
1302 azx_free_cmd_io(chip);
1303 /* disable unsolicited responses */
1304 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL);
1305 return -1;
1306}
1307
1308/*
1309 * Use the single immediate command instead of CORB/RIRB for simplicity
1310 *
1311 * Note: according to Intel, this is not preferred use. The command was
1312 * intended for the BIOS only, and may get confused with unsolicited
1313 * responses. So, we shouldn't use it for normal operation from the
1314 * driver.
1315 * I left the codes, however, for debugging/testing purposes.
1316 */
1317
1318/* receive a response */
1319static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
1320{
1321 int timeout = 50;
1322
1323 while (timeout--) {
1324 /* check IRV busy bit */
1325 if (azx_readw(chip, IRS) & ICH6_IRS_VALID) {
1326 /* reuse rirb.res as the response return value */
1327 chip->rirb.res[addr] = azx_readl(chip, IR);
1328 return 0;
1329 }
1330 udelay(1);
1331 }
1332 if (printk_ratelimit())
1333 dev_dbg(chip->card->dev, "get_response timeout: IRS=0x%x\n",
1334 azx_readw(chip, IRS));
1335 chip->rirb.res[addr] = -1;
1336 return -EIO;
1337}
1338
1339/* send a command */
1340static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
1341{
1342 struct azx *chip = bus->private_data;
1343 unsigned int addr = azx_command_addr(val);
1344 int timeout = 50;
1345
1346 bus->rirb_error = 0;
1347 while (timeout--) {
1348 /* check ICB busy bit */
1349 if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
1350 /* Clear IRV valid bit */
1351 azx_writew(chip, IRS, azx_readw(chip, IRS) |
1352 ICH6_IRS_VALID);
1353 azx_writel(chip, IC, val);
1354 azx_writew(chip, IRS, azx_readw(chip, IRS) |
1355 ICH6_IRS_BUSY);
1356 return azx_single_wait_for_response(chip, addr);
1357 }
1358 udelay(1);
1359 }
1360 if (printk_ratelimit())
1361 dev_dbg(chip->card->dev,
1362 "send_cmd timeout: IRS=0x%x, val=0x%x\n",
1363 azx_readw(chip, IRS), val);
1364 return -EIO;
1365}
1366
1367/* receive a response */
1368static unsigned int azx_single_get_response(struct hda_bus *bus,
1369 unsigned int addr)
1370{
1371 struct azx *chip = bus->private_data;
1372 return chip->rirb.res[addr];
1373}
1374
1375/*
1376 * The below are the main callbacks from hda_codec.
1377 *
1378 * They are just the skeleton to call sub-callbacks according to the
1379 * current setting of chip->single_cmd.
1380 */
1381
1382/* send a command */
1383int azx_send_cmd(struct hda_bus *bus, unsigned int val)
1384{
1385 struct azx *chip = bus->private_data;
1386
1387 if (chip->disabled)
1388 return 0;
1389 chip->last_cmd[azx_command_addr(val)] = val;
1390 if (chip->single_cmd)
1391 return azx_single_send_cmd(bus, val);
1392 else
1393 return azx_corb_send_cmd(bus, val);
1394}
1395EXPORT_SYMBOL_GPL(azx_send_cmd);
1396
1397/* get a response */
1398unsigned int azx_get_response(struct hda_bus *bus,
1399 unsigned int addr)
1400{
1401 struct azx *chip = bus->private_data;
1402 if (chip->disabled)
1403 return 0;
1404 if (chip->single_cmd)
1405 return azx_single_get_response(bus, addr);
1406 else
1407 return azx_rirb_get_response(bus, addr);
1408}
1409EXPORT_SYMBOL_GPL(azx_get_response);
1410
1026#ifdef CONFIG_SND_HDA_DSP_LOADER 1411#ifdef CONFIG_SND_HDA_DSP_LOADER
1027/* 1412/*
1028 * DSP loading code (e.g. for CA0132) 1413 * DSP loading code (e.g. for CA0132)
diff --git a/sound/pci/hda/hda_controller.h b/sound/pci/hda/hda_controller.h
index 7c9c04d23f20..fb0cdddc356a 100644
--- a/sound/pci/hda/hda_controller.h
+++ b/sound/pci/hda/hda_controller.h
@@ -47,4 +47,15 @@ void azx_load_dsp_cleanup(struct hda_bus *bus,
47int azx_alloc_stream_pages(struct azx *chip); 47int azx_alloc_stream_pages(struct azx *chip);
48void azx_free_stream_pages(struct azx *chip); 48void azx_free_stream_pages(struct azx *chip);
49 49
50/*
51 * CORB / RIRB interface
52 */
53int azx_alloc_cmd_io(struct azx *chip);
54void azx_init_cmd_io(struct azx *chip);
55void azx_free_cmd_io(struct azx *chip);
56void azx_update_rirb(struct azx *chip);
57int azx_send_cmd(struct hda_bus *bus, unsigned int val);
58unsigned int azx_get_response(struct hda_bus *bus,
59 unsigned int addr);
60
50#endif /* __SOUND_HDA_CONTROLLER_H */ 61#endif /* __SOUND_HDA_CONTROLLER_H */
diff --git a/sound/pci/hda/hda_intel.c b/sound/pci/hda/hda_intel.c
index 80250b3a6fc3..a8af3d4ca4be 100644
--- a/sound/pci/hda/hda_intel.c
+++ b/sound/pci/hda/hda_intel.c
@@ -335,389 +335,6 @@ static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
335#endif 335#endif
336 336
337static int azx_acquire_irq(struct azx *chip, int do_disconnect); 337static int azx_acquire_irq(struct azx *chip, int do_disconnect);
338static int azx_send_cmd(struct hda_bus *bus, unsigned int val);
339/*
340 * Interface for HD codec
341 */
342
343/*
344 * CORB / RIRB interface
345 */
346static int azx_alloc_cmd_io(struct azx *chip)
347{
348 int err;
349
350 /* single page (at least 4096 bytes) must suffice for both ringbuffes */
351 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV,
352 PAGE_SIZE, &chip->rb);
353 if (err < 0)
354 dev_err(chip->card->dev, "cannot allocate CORB/RIRB\n");
355 return err;
356}
357
358static void azx_init_cmd_io(struct azx *chip)
359{
360 int timeout;
361
362 spin_lock_irq(&chip->reg_lock);
363 /* CORB set up */
364 chip->corb.addr = chip->rb.addr;
365 chip->corb.buf = (u32 *)chip->rb.area;
366 azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
367 azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
368
369 /* set the corb size to 256 entries (ULI requires explicitly) */
370 azx_writeb(chip, CORBSIZE, 0x02);
371 /* set the corb write pointer to 0 */
372 azx_writew(chip, CORBWP, 0);
373
374 /* reset the corb hw read pointer */
375 azx_writew(chip, CORBRP, ICH6_CORBRP_RST);
376 for (timeout = 1000; timeout > 0; timeout--) {
377 if ((azx_readw(chip, CORBRP) & ICH6_CORBRP_RST) == ICH6_CORBRP_RST)
378 break;
379 udelay(1);
380 }
381 if (timeout <= 0)
382 dev_err(chip->card->dev, "CORB reset timeout#1, CORBRP = %d\n",
383 azx_readw(chip, CORBRP));
384
385 azx_writew(chip, CORBRP, 0);
386 for (timeout = 1000; timeout > 0; timeout--) {
387 if (azx_readw(chip, CORBRP) == 0)
388 break;
389 udelay(1);
390 }
391 if (timeout <= 0)
392 dev_err(chip->card->dev, "CORB reset timeout#2, CORBRP = %d\n",
393 azx_readw(chip, CORBRP));
394
395 /* enable corb dma */
396 azx_writeb(chip, CORBCTL, ICH6_CORBCTL_RUN);
397
398 /* RIRB set up */
399 chip->rirb.addr = chip->rb.addr + 2048;
400 chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
401 chip->rirb.wp = chip->rirb.rp = 0;
402 memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
403 azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
404 azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
405
406 /* set the rirb size to 256 entries (ULI requires explicitly) */
407 azx_writeb(chip, RIRBSIZE, 0x02);
408 /* reset the rirb hw write pointer */
409 azx_writew(chip, RIRBWP, ICH6_RIRBWP_RST);
410 /* set N=1, get RIRB response interrupt for new entry */
411 if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
412 azx_writew(chip, RINTCNT, 0xc0);
413 else
414 azx_writew(chip, RINTCNT, 1);
415 /* enable rirb dma and response irq */
416 azx_writeb(chip, RIRBCTL, ICH6_RBCTL_DMA_EN | ICH6_RBCTL_IRQ_EN);
417 spin_unlock_irq(&chip->reg_lock);
418}
419
420static void azx_free_cmd_io(struct azx *chip)
421{
422 spin_lock_irq(&chip->reg_lock);
423 /* disable ringbuffer DMAs */
424 azx_writeb(chip, RIRBCTL, 0);
425 azx_writeb(chip, CORBCTL, 0);
426 spin_unlock_irq(&chip->reg_lock);
427}
428
429static unsigned int azx_command_addr(u32 cmd)
430{
431 unsigned int addr = cmd >> 28;
432
433 if (addr >= AZX_MAX_CODECS) {
434 snd_BUG();
435 addr = 0;
436 }
437
438 return addr;
439}
440
441/* send a command */
442static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
443{
444 struct azx *chip = bus->private_data;
445 unsigned int addr = azx_command_addr(val);
446 unsigned int wp, rp;
447
448 spin_lock_irq(&chip->reg_lock);
449
450 /* add command to corb */
451 wp = azx_readw(chip, CORBWP);
452 if (wp == 0xffff) {
453 /* something wrong, controller likely turned to D3 */
454 spin_unlock_irq(&chip->reg_lock);
455 return -EIO;
456 }
457 wp++;
458 wp %= ICH6_MAX_CORB_ENTRIES;
459
460 rp = azx_readw(chip, CORBRP);
461 if (wp == rp) {
462 /* oops, it's full */
463 spin_unlock_irq(&chip->reg_lock);
464 return -EAGAIN;
465 }
466
467 chip->rirb.cmds[addr]++;
468 chip->corb.buf[wp] = cpu_to_le32(val);
469 azx_writew(chip, CORBWP, wp);
470
471 spin_unlock_irq(&chip->reg_lock);
472
473 return 0;
474}
475
476#define ICH6_RIRB_EX_UNSOL_EV (1<<4)
477
478/* retrieve RIRB entry - called from interrupt handler */
479static void azx_update_rirb(struct azx *chip)
480{
481 unsigned int rp, wp;
482 unsigned int addr;
483 u32 res, res_ex;
484
485 wp = azx_readw(chip, RIRBWP);
486 if (wp == 0xffff) {
487 /* something wrong, controller likely turned to D3 */
488 return;
489 }
490
491 if (wp == chip->rirb.wp)
492 return;
493 chip->rirb.wp = wp;
494
495 while (chip->rirb.rp != wp) {
496 chip->rirb.rp++;
497 chip->rirb.rp %= ICH6_MAX_RIRB_ENTRIES;
498
499 rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
500 res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
501 res = le32_to_cpu(chip->rirb.buf[rp]);
502 addr = res_ex & 0xf;
503 if ((addr >= AZX_MAX_CODECS) || !(chip->codec_mask & (1 << addr))) {
504 dev_err(chip->card->dev, "spurious response %#x:%#x, rp = %d, wp = %d",
505 res, res_ex,
506 chip->rirb.rp, wp);
507 snd_BUG();
508 }
509 else if (res_ex & ICH6_RIRB_EX_UNSOL_EV)
510 snd_hda_queue_unsol_event(chip->bus, res, res_ex);
511 else if (chip->rirb.cmds[addr]) {
512 chip->rirb.res[addr] = res;
513 smp_wmb();
514 chip->rirb.cmds[addr]--;
515 } else if (printk_ratelimit()) {
516 dev_err(chip->card->dev, "spurious response %#x:%#x, last cmd=%#08x\n",
517 res, res_ex,
518 chip->last_cmd[addr]);
519 }
520 }
521}
522
523/* receive a response */
524static unsigned int azx_rirb_get_response(struct hda_bus *bus,
525 unsigned int addr)
526{
527 struct azx *chip = bus->private_data;
528 unsigned long timeout;
529 unsigned long loopcounter;
530 int do_poll = 0;
531
532 again:
533 timeout = jiffies + msecs_to_jiffies(1000);
534
535 for (loopcounter = 0;; loopcounter++) {
536 if (chip->polling_mode || do_poll) {
537 spin_lock_irq(&chip->reg_lock);
538 azx_update_rirb(chip);
539 spin_unlock_irq(&chip->reg_lock);
540 }
541 if (!chip->rirb.cmds[addr]) {
542 smp_rmb();
543 bus->rirb_error = 0;
544
545 if (!do_poll)
546 chip->poll_count = 0;
547 return chip->rirb.res[addr]; /* the last value */
548 }
549 if (time_after(jiffies, timeout))
550 break;
551 if (bus->needs_damn_long_delay || loopcounter > 3000)
552 msleep(2); /* temporary workaround */
553 else {
554 udelay(10);
555 cond_resched();
556 }
557 }
558
559 if (!bus->no_response_fallback)
560 return -1;
561
562 if (!chip->polling_mode && chip->poll_count < 2) {
563 dev_dbg(chip->card->dev,
564 "azx_get_response timeout, polling the codec once: last cmd=0x%08x\n",
565 chip->last_cmd[addr]);
566 do_poll = 1;
567 chip->poll_count++;
568 goto again;
569 }
570
571
572 if (!chip->polling_mode) {
573 dev_warn(chip->card->dev,
574 "azx_get_response timeout, switching to polling mode: last cmd=0x%08x\n",
575 chip->last_cmd[addr]);
576 chip->polling_mode = 1;
577 goto again;
578 }
579
580 if (chip->msi) {
581 dev_warn(chip->card->dev,
582 "No response from codec, disabling MSI: last cmd=0x%08x\n",
583 chip->last_cmd[addr]);
584 if (chip->ops->disable_msi_reset_irq &&
585 chip->ops->disable_msi_reset_irq(chip) < 0) {
586 bus->rirb_error = 1;
587 return -1;
588 }
589 goto again;
590 }
591
592 if (chip->probing) {
593 /* If this critical timeout happens during the codec probing
594 * phase, this is likely an access to a non-existing codec
595 * slot. Better to return an error and reset the system.
596 */
597 return -1;
598 }
599
600 /* a fatal communication error; need either to reset or to fallback
601 * to the single_cmd mode
602 */
603 bus->rirb_error = 1;
604 if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
605 bus->response_reset = 1;
606 return -1; /* give a chance to retry */
607 }
608
609 dev_err(chip->card->dev,
610 "azx_get_response timeout, switching to single_cmd mode: last cmd=0x%08x\n",
611 chip->last_cmd[addr]);
612 chip->single_cmd = 1;
613 bus->response_reset = 0;
614 /* release CORB/RIRB */
615 azx_free_cmd_io(chip);
616 /* disable unsolicited responses */
617 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~ICH6_GCTL_UNSOL);
618 return -1;
619}
620
621/*
622 * Use the single immediate command instead of CORB/RIRB for simplicity
623 *
624 * Note: according to Intel, this is not preferred use. The command was
625 * intended for the BIOS only, and may get confused with unsolicited
626 * responses. So, we shouldn't use it for normal operation from the
627 * driver.
628 * I left the codes, however, for debugging/testing purposes.
629 */
630
631/* receive a response */
632static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
633{
634 int timeout = 50;
635
636 while (timeout--) {
637 /* check IRV busy bit */
638 if (azx_readw(chip, IRS) & ICH6_IRS_VALID) {
639 /* reuse rirb.res as the response return value */
640 chip->rirb.res[addr] = azx_readl(chip, IR);
641 return 0;
642 }
643 udelay(1);
644 }
645 if (printk_ratelimit())
646 dev_dbg(chip->card->dev, "get_response timeout: IRS=0x%x\n",
647 azx_readw(chip, IRS));
648 chip->rirb.res[addr] = -1;
649 return -EIO;
650}
651
652/* send a command */
653static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
654{
655 struct azx *chip = bus->private_data;
656 unsigned int addr = azx_command_addr(val);
657 int timeout = 50;
658
659 bus->rirb_error = 0;
660 while (timeout--) {
661 /* check ICB busy bit */
662 if (!((azx_readw(chip, IRS) & ICH6_IRS_BUSY))) {
663 /* Clear IRV valid bit */
664 azx_writew(chip, IRS, azx_readw(chip, IRS) |
665 ICH6_IRS_VALID);
666 azx_writel(chip, IC, val);
667 azx_writew(chip, IRS, azx_readw(chip, IRS) |
668 ICH6_IRS_BUSY);
669 return azx_single_wait_for_response(chip, addr);
670 }
671 udelay(1);
672 }
673 if (printk_ratelimit())
674 dev_dbg(chip->card->dev,
675 "send_cmd timeout: IRS=0x%x, val=0x%x\n",
676 azx_readw(chip, IRS), val);
677 return -EIO;
678}
679
680/* receive a response */
681static unsigned int azx_single_get_response(struct hda_bus *bus,
682 unsigned int addr)
683{
684 struct azx *chip = bus->private_data;
685 return chip->rirb.res[addr];
686}
687
688/*
689 * The below are the main callbacks from hda_codec.
690 *
691 * They are just the skeleton to call sub-callbacks according to the
692 * current setting of chip->single_cmd.
693 */
694
695/* send a command */
696static int azx_send_cmd(struct hda_bus *bus, unsigned int val)
697{
698 struct azx *chip = bus->private_data;
699
700 if (chip->disabled)
701 return 0;
702 chip->last_cmd[azx_command_addr(val)] = val;
703 if (chip->single_cmd)
704 return azx_single_send_cmd(bus, val);
705 else
706 return azx_corb_send_cmd(bus, val);
707}
708
709/* get a response */
710static unsigned int azx_get_response(struct hda_bus *bus,
711 unsigned int addr)
712{
713 struct azx *chip = bus->private_data;
714 if (chip->disabled)
715 return 0;
716 if (chip->single_cmd)
717 return azx_single_get_response(bus, addr);
718 else
719 return azx_rirb_get_response(bus, addr);
720}
721 338
722#ifdef CONFIG_PM 339#ifdef CONFIG_PM
723static void azx_power_notify(struct hda_bus *bus, bool power_up); 340static void azx_power_notify(struct hda_bus *bus, bool power_up);