cx18-firmware.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * cx18 firmware functions
  3. *
  4. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. * 02111-1307 USA
  20. */
  21. #include "cx18-driver.h"
  22. #include "cx18-io.h"
  23. #include "cx18-scb.h"
  24. #include "cx18-irq.h"
  25. #include "cx18-firmware.h"
  26. #include "cx18-cards.h"
  27. #include <linux/firmware.h>
  28. #define CX18_PROC_SOFT_RESET 0xc70010
  29. #define CX18_DDR_SOFT_RESET 0xc70014
  30. #define CX18_CLOCK_SELECT1 0xc71000
  31. #define CX18_CLOCK_SELECT2 0xc71004
  32. #define CX18_HALF_CLOCK_SELECT1 0xc71008
  33. #define CX18_HALF_CLOCK_SELECT2 0xc7100C
  34. #define CX18_CLOCK_POLARITY1 0xc71010
  35. #define CX18_CLOCK_POLARITY2 0xc71014
  36. #define CX18_ADD_DELAY_ENABLE1 0xc71018
  37. #define CX18_ADD_DELAY_ENABLE2 0xc7101C
  38. #define CX18_CLOCK_ENABLE1 0xc71020
  39. #define CX18_CLOCK_ENABLE2 0xc71024
  40. #define CX18_REG_BUS_TIMEOUT_EN 0xc72024
  41. #define CX18_FAST_CLOCK_PLL_INT 0xc78000
  42. #define CX18_FAST_CLOCK_PLL_FRAC 0xc78004
  43. #define CX18_FAST_CLOCK_PLL_POST 0xc78008
  44. #define CX18_FAST_CLOCK_PLL_PRESCALE 0xc7800C
  45. #define CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH 0xc78010
  46. #define CX18_SLOW_CLOCK_PLL_INT 0xc78014
  47. #define CX18_SLOW_CLOCK_PLL_FRAC 0xc78018
  48. #define CX18_SLOW_CLOCK_PLL_POST 0xc7801C
  49. #define CX18_MPEG_CLOCK_PLL_INT 0xc78040
  50. #define CX18_MPEG_CLOCK_PLL_FRAC 0xc78044
  51. #define CX18_MPEG_CLOCK_PLL_POST 0xc78048
  52. #define CX18_PLL_POWER_DOWN 0xc78088
  53. #define CX18_SW1_INT_STATUS 0xc73104
  54. #define CX18_SW1_INT_ENABLE_PCI 0xc7311C
  55. #define CX18_SW2_INT_SET 0xc73140
  56. #define CX18_SW2_INT_STATUS 0xc73144
  57. #define CX18_ADEC_CONTROL 0xc78120
  58. #define CX18_DDR_REQUEST_ENABLE 0xc80000
  59. #define CX18_DDR_CHIP_CONFIG 0xc80004
  60. #define CX18_DDR_REFRESH 0xc80008
  61. #define CX18_DDR_TIMING1 0xc8000C
  62. #define CX18_DDR_TIMING2 0xc80010
  63. #define CX18_DDR_POWER_REG 0xc8001C
  64. #define CX18_DDR_TUNE_LANE 0xc80048
  65. #define CX18_DDR_INITIAL_EMRS 0xc80054
  66. #define CX18_DDR_MB_PER_ROW_7 0xc8009C
  67. #define CX18_DDR_BASE_63_ADDR 0xc804FC
  68. #define CX18_WMB_CLIENT02 0xc90108
  69. #define CX18_WMB_CLIENT05 0xc90114
  70. #define CX18_WMB_CLIENT06 0xc90118
  71. #define CX18_WMB_CLIENT07 0xc9011C
  72. #define CX18_WMB_CLIENT08 0xc90120
  73. #define CX18_WMB_CLIENT09 0xc90124
  74. #define CX18_WMB_CLIENT10 0xc90128
  75. #define CX18_WMB_CLIENT11 0xc9012C
  76. #define CX18_WMB_CLIENT12 0xc90130
  77. #define CX18_WMB_CLIENT13 0xc90134
  78. #define CX18_WMB_CLIENT14 0xc90138
  79. #define CX18_DSP0_INTERRUPT_MASK 0xd0004C
  80. #define APU_ROM_SYNC1 0x6D676553 /* "mgeS" */
  81. #define APU_ROM_SYNC2 0x72646548 /* "rdeH" */
  82. struct cx18_apu_rom_seghdr {
  83. u32 sync1;
  84. u32 sync2;
  85. u32 addr;
  86. u32 size;
  87. };
  88. static int load_cpu_fw_direct(const char *fn, u8 __iomem *mem, struct cx18 *cx)
  89. {
  90. const struct firmware *fw = NULL;
  91. int i, j;
  92. unsigned size;
  93. u32 __iomem *dst = (u32 __iomem *)mem;
  94. const u32 *src;
  95. if (request_firmware(&fw, fn, &cx->dev->dev)) {
  96. CX18_ERR("Unable to open firmware %s\n", fn);
  97. CX18_ERR("Did you put the firmware in the hotplug firmware directory?\n");
  98. return -ENOMEM;
  99. }
  100. src = (const u32 *)fw->data;
  101. for (i = 0; i < fw->size; i += 4096) {
  102. cx18_setup_page(cx, i);
  103. for (j = i; j < fw->size && j < i + 4096; j += 4) {
  104. /* no need for endianness conversion on the ppc */
  105. cx18_raw_writel(cx, *src, dst);
  106. if (cx18_raw_readl(cx, dst) != *src) {
  107. CX18_ERR("Mismatch at offset %x\n", i);
  108. release_firmware(fw);
  109. return -EIO;
  110. }
  111. dst++;
  112. src++;
  113. }
  114. }
  115. if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))
  116. CX18_INFO("loaded %s firmware (%zd bytes)\n", fn, fw->size);
  117. size = fw->size;
  118. release_firmware(fw);
  119. return size;
  120. }
  121. static int load_apu_fw_direct(const char *fn, u8 __iomem *dst, struct cx18 *cx,
  122. u32 *entry_addr)
  123. {
  124. const struct firmware *fw = NULL;
  125. int i, j;
  126. unsigned size;
  127. const u32 *src;
  128. struct cx18_apu_rom_seghdr seghdr;
  129. const u8 *vers;
  130. u32 offset = 0;
  131. u32 apu_version = 0;
  132. int sz;
  133. if (request_firmware(&fw, fn, &cx->dev->dev)) {
  134. CX18_ERR("unable to open firmware %s\n", fn);
  135. CX18_ERR("did you put the firmware in the hotplug firmware directory?\n");
  136. return -ENOMEM;
  137. }
  138. *entry_addr = 0xffffffff;
  139. src = (const u32 *)fw->data;
  140. vers = fw->data + sizeof(seghdr);
  141. sz = fw->size;
  142. apu_version = (vers[0] << 24) | (vers[4] << 16) | vers[32];
  143. while (offset + sizeof(seghdr) < fw->size) {
  144. /* TODO: byteswapping */
  145. memcpy(&seghdr, src + offset / 4, sizeof(seghdr));
  146. offset += sizeof(seghdr);
  147. if (seghdr.sync1 != APU_ROM_SYNC1 ||
  148. seghdr.sync2 != APU_ROM_SYNC2) {
  149. offset += seghdr.size;
  150. continue;
  151. }
  152. CX18_DEBUG_INFO("load segment %x-%x\n", seghdr.addr,
  153. seghdr.addr + seghdr.size - 1);
  154. if (*entry_addr == 0xffffffff)
  155. *entry_addr = seghdr.addr;
  156. if (offset + seghdr.size > sz)
  157. break;
  158. for (i = 0; i < seghdr.size; i += 4096) {
  159. cx18_setup_page(cx, seghdr.addr + i);
  160. for (j = i; j < seghdr.size && j < i + 4096; j += 4) {
  161. /* no need for endianness conversion on the ppc */
  162. cx18_raw_writel(cx, src[(offset + j) / 4],
  163. dst + seghdr.addr + j);
  164. if (cx18_raw_readl(cx, dst + seghdr.addr + j)
  165. != src[(offset + j) / 4]) {
  166. CX18_ERR("Mismatch at offset %x\n",
  167. offset + j);
  168. release_firmware(fw);
  169. return -EIO;
  170. }
  171. }
  172. }
  173. offset += seghdr.size;
  174. }
  175. if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))
  176. CX18_INFO("loaded %s firmware V%08x (%zd bytes)\n",
  177. fn, apu_version, fw->size);
  178. size = fw->size;
  179. release_firmware(fw);
  180. return size;
  181. }
  182. void cx18_halt_firmware(struct cx18 *cx)
  183. {
  184. CX18_DEBUG_INFO("Preparing for firmware halt.\n");
  185. cx18_write_reg_expect(cx, 0x000F000F, CX18_PROC_SOFT_RESET,
  186. 0x0000000F, 0x000F000F);
  187. cx18_write_reg_expect(cx, 0x00020002, CX18_ADEC_CONTROL,
  188. 0x00000002, 0x00020002);
  189. }
  190. void cx18_init_power(struct cx18 *cx, int lowpwr)
  191. {
  192. /* power-down Spare and AOM PLLs */
  193. /* power-up fast, slow and mpeg PLLs */
  194. cx18_write_reg(cx, 0x00000008, CX18_PLL_POWER_DOWN);
  195. /* ADEC out of sleep */
  196. cx18_write_reg_expect(cx, 0x00020000, CX18_ADEC_CONTROL,
  197. 0x00000000, 0x00020002);
  198. /* The fast clock is at 200/245 MHz */
  199. cx18_write_reg(cx, lowpwr ? 0xD : 0x11, CX18_FAST_CLOCK_PLL_INT);
  200. cx18_write_reg(cx, lowpwr ? 0x1EFBF37 : 0x038E3D7,
  201. CX18_FAST_CLOCK_PLL_FRAC);
  202. cx18_write_reg(cx, 2, CX18_FAST_CLOCK_PLL_POST);
  203. cx18_write_reg(cx, 1, CX18_FAST_CLOCK_PLL_PRESCALE);
  204. cx18_write_reg(cx, 4, CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH);
  205. /* set slow clock to 125/120 MHz */
  206. cx18_write_reg(cx, lowpwr ? 0x11 : 0x10, CX18_SLOW_CLOCK_PLL_INT);
  207. cx18_write_reg(cx, lowpwr ? 0xEBAF05 : 0x18618A8,
  208. CX18_SLOW_CLOCK_PLL_FRAC);
  209. cx18_write_reg(cx, 4, CX18_SLOW_CLOCK_PLL_POST);
  210. /* mpeg clock pll 54MHz */
  211. cx18_write_reg(cx, 0xF, CX18_MPEG_CLOCK_PLL_INT);
  212. cx18_write_reg(cx, 0x2BCFEF, CX18_MPEG_CLOCK_PLL_FRAC);
  213. cx18_write_reg(cx, 8, CX18_MPEG_CLOCK_PLL_POST);
  214. /* Defaults */
  215. /* APU = SC or SC/2 = 125/62.5 */
  216. /* EPU = SC = 125 */
  217. /* DDR = FC = 180 */
  218. /* ENC = SC = 125 */
  219. /* AI1 = SC = 125 */
  220. /* VIM2 = disabled */
  221. /* PCI = FC/2 = 90 */
  222. /* AI2 = disabled */
  223. /* DEMUX = disabled */
  224. /* AO = SC/2 = 62.5 */
  225. /* SER = 54MHz */
  226. /* VFC = disabled */
  227. /* USB = disabled */
  228. if (lowpwr) {
  229. cx18_write_reg_expect(cx, 0xFFFF0020, CX18_CLOCK_SELECT1,
  230. 0x00000020, 0xFFFFFFFF);
  231. cx18_write_reg_expect(cx, 0xFFFF0004, CX18_CLOCK_SELECT2,
  232. 0x00000004, 0xFFFFFFFF);
  233. } else {
  234. /* This doesn't explicitly set every clock select */
  235. cx18_write_reg_expect(cx, 0x00060004, CX18_CLOCK_SELECT1,
  236. 0x00000004, 0x00060006);
  237. cx18_write_reg_expect(cx, 0x00060006, CX18_CLOCK_SELECT2,
  238. 0x00000006, 0x00060006);
  239. }
  240. cx18_write_reg_expect(cx, 0xFFFF0002, CX18_HALF_CLOCK_SELECT1,
  241. 0x00000002, 0xFFFFFFFF);
  242. cx18_write_reg_expect(cx, 0xFFFF0104, CX18_HALF_CLOCK_SELECT2,
  243. 0x00000104, 0xFFFFFFFF);
  244. cx18_write_reg_expect(cx, 0xFFFF9026, CX18_CLOCK_ENABLE1,
  245. 0x00009026, 0xFFFFFFFF);
  246. cx18_write_reg_expect(cx, 0xFFFF3105, CX18_CLOCK_ENABLE2,
  247. 0x00003105, 0xFFFFFFFF);
  248. }
  249. void cx18_init_memory(struct cx18 *cx)
  250. {
  251. cx18_msleep_timeout(10, 0);
  252. cx18_write_reg_expect(cx, 0x00010000, CX18_DDR_SOFT_RESET,
  253. 0x00000000, 0x00010001);
  254. cx18_msleep_timeout(10, 0);
  255. cx18_write_reg(cx, cx->card->ddr.chip_config, CX18_DDR_CHIP_CONFIG);
  256. cx18_msleep_timeout(10, 0);
  257. cx18_write_reg(cx, cx->card->ddr.refresh, CX18_DDR_REFRESH);
  258. cx18_write_reg(cx, cx->card->ddr.timing1, CX18_DDR_TIMING1);
  259. cx18_write_reg(cx, cx->card->ddr.timing2, CX18_DDR_TIMING2);
  260. cx18_msleep_timeout(10, 0);
  261. /* Initialize DQS pad time */
  262. cx18_write_reg(cx, cx->card->ddr.tune_lane, CX18_DDR_TUNE_LANE);
  263. cx18_write_reg(cx, cx->card->ddr.initial_emrs, CX18_DDR_INITIAL_EMRS);
  264. cx18_msleep_timeout(10, 0);
  265. cx18_write_reg_expect(cx, 0x00020000, CX18_DDR_SOFT_RESET,
  266. 0x00000000, 0x00020002);
  267. cx18_msleep_timeout(10, 0);
  268. /* use power-down mode when idle */
  269. cx18_write_reg(cx, 0x00000010, CX18_DDR_POWER_REG);
  270. cx18_write_reg_expect(cx, 0x00010001, CX18_REG_BUS_TIMEOUT_EN,
  271. 0x00000001, 0x00010001);
  272. cx18_write_reg(cx, 0x48, CX18_DDR_MB_PER_ROW_7);
  273. cx18_write_reg(cx, 0xE0000, CX18_DDR_BASE_63_ADDR);
  274. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT02); /* AO */
  275. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT09); /* AI2 */
  276. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT05); /* VIM1 */
  277. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT06); /* AI1 */
  278. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT07); /* 3D comb */
  279. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT10); /* ME */
  280. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT12); /* ENC */
  281. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT13); /* PK */
  282. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT11); /* RC */
  283. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT14); /* AVO */
  284. }
  285. int cx18_firmware_init(struct cx18 *cx)
  286. {
  287. /* Allow chip to control CLKRUN */
  288. cx18_write_reg(cx, 0x5, CX18_DSP0_INTERRUPT_MASK);
  289. /* Stop the firmware */
  290. cx18_write_reg_expect(cx, 0x000F000F, CX18_PROC_SOFT_RESET,
  291. 0x0000000F, 0x000F000F);
  292. cx18_msleep_timeout(1, 0);
  293. cx18_sw1_irq_enable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
  294. cx18_sw2_irq_enable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
  295. /* Only if the processor is not running */
  296. if (cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 8) {
  297. u32 fw_entry_addr;
  298. int sz = load_apu_fw_direct("v4l-cx23418-apu.fw",
  299. cx->enc_mem, cx, &fw_entry_addr);
  300. /* Clear bit0 for APU to start from 0 */
  301. cx18_write_reg(cx, cx18_read_reg(cx, 0xc72030) & ~1, 0xc72030);
  302. cx18_write_enc(cx, 0xE51FF004, 0); /* ldr pc, [pc, #-4] */
  303. cx18_write_enc(cx, fw_entry_addr, 4);
  304. /* Start APU */
  305. cx18_write_reg_expect(cx, 0x00010000, CX18_PROC_SOFT_RESET,
  306. 0x00000000, 0x00010001);
  307. cx18_msleep_timeout(500, 0);
  308. sz = sz <= 0 ? sz : load_cpu_fw_direct("v4l-cx23418-cpu.fw",
  309. cx->enc_mem, cx);
  310. if (sz > 0) {
  311. int retries = 0;
  312. /* start the CPU */
  313. cx18_write_reg_expect(cx,
  314. 0x00080000, CX18_PROC_SOFT_RESET,
  315. 0x00000000, 0x00080008);
  316. while (retries++ < 50) { /* Loop for max 500mS */
  317. if ((cx18_read_reg(cx, CX18_PROC_SOFT_RESET)
  318. & 1) == 0)
  319. break;
  320. cx18_msleep_timeout(10, 0);
  321. }
  322. cx18_msleep_timeout(200, 0);
  323. if (retries == 51) {
  324. CX18_ERR("Could not start the CPU\n");
  325. return -EIO;
  326. }
  327. }
  328. if (sz <= 0)
  329. return -EIO;
  330. }
  331. /* initialize GPIO */
  332. cx18_write_reg_expect(cx, 0x14001400, 0xc78110, 0x00001400, 0x14001400);
  333. return 0;
  334. }