cx18-firmware.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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-scb.h"
  23. #include "cx18-irq.h"
  24. #include "cx18-firmware.h"
  25. #include "cx18-cards.h"
  26. #include <linux/firmware.h>
  27. #define CX18_PROC_SOFT_RESET 0xc70010
  28. #define CX18_DDR_SOFT_RESET 0xc70014
  29. #define CX18_CLOCK_SELECT1 0xc71000
  30. #define CX18_CLOCK_SELECT2 0xc71004
  31. #define CX18_HALF_CLOCK_SELECT1 0xc71008
  32. #define CX18_HALF_CLOCK_SELECT2 0xc7100C
  33. #define CX18_CLOCK_POLARITY1 0xc71010
  34. #define CX18_CLOCK_POLARITY2 0xc71014
  35. #define CX18_ADD_DELAY_ENABLE1 0xc71018
  36. #define CX18_ADD_DELAY_ENABLE2 0xc7101C
  37. #define CX18_CLOCK_ENABLE1 0xc71020
  38. #define CX18_CLOCK_ENABLE2 0xc71024
  39. #define CX18_REG_BUS_TIMEOUT_EN 0xc72024
  40. #define CX18_AUDIO_ENABLE 0xc72014
  41. #define CX18_REG_BUS_TIMEOUT_EN 0xc72024
  42. #define CX18_FAST_CLOCK_PLL_INT 0xc78000
  43. #define CX18_FAST_CLOCK_PLL_FRAC 0xc78004
  44. #define CX18_FAST_CLOCK_PLL_POST 0xc78008
  45. #define CX18_FAST_CLOCK_PLL_PRESCALE 0xc7800C
  46. #define CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH 0xc78010
  47. #define CX18_SLOW_CLOCK_PLL_INT 0xc78014
  48. #define CX18_SLOW_CLOCK_PLL_FRAC 0xc78018
  49. #define CX18_SLOW_CLOCK_PLL_POST 0xc7801C
  50. #define CX18_MPEG_CLOCK_PLL_INT 0xc78040
  51. #define CX18_MPEG_CLOCK_PLL_FRAC 0xc78044
  52. #define CX18_MPEG_CLOCK_PLL_POST 0xc78048
  53. #define CX18_PLL_POWER_DOWN 0xc78088
  54. #define CX18_SW1_INT_STATUS 0xc73104
  55. #define CX18_SW1_INT_ENABLE_PCI 0xc7311C
  56. #define CX18_SW2_INT_SET 0xc73140
  57. #define CX18_SW2_INT_STATUS 0xc73144
  58. #define CX18_ADEC_CONTROL 0xc78120
  59. #define CX18_DDR_REQUEST_ENABLE 0xc80000
  60. #define CX18_DDR_CHIP_CONFIG 0xc80004
  61. #define CX18_DDR_REFRESH 0xc80008
  62. #define CX18_DDR_TIMING1 0xc8000C
  63. #define CX18_DDR_TIMING2 0xc80010
  64. #define CX18_DDR_POWER_REG 0xc8001C
  65. #define CX18_DDR_TUNE_LANE 0xc80048
  66. #define CX18_DDR_INITIAL_EMRS 0xc80054
  67. #define CX18_DDR_MB_PER_ROW_7 0xc8009C
  68. #define CX18_DDR_BASE_63_ADDR 0xc804FC
  69. #define CX18_WMB_CLIENT02 0xc90108
  70. #define CX18_WMB_CLIENT05 0xc90114
  71. #define CX18_WMB_CLIENT06 0xc90118
  72. #define CX18_WMB_CLIENT07 0xc9011C
  73. #define CX18_WMB_CLIENT08 0xc90120
  74. #define CX18_WMB_CLIENT09 0xc90124
  75. #define CX18_WMB_CLIENT10 0xc90128
  76. #define CX18_WMB_CLIENT11 0xc9012C
  77. #define CX18_WMB_CLIENT12 0xc90130
  78. #define CX18_WMB_CLIENT13 0xc90134
  79. #define CX18_WMB_CLIENT14 0xc90138
  80. #define CX18_DSP0_INTERRUPT_MASK 0xd0004C
  81. /* Encoder/decoder firmware sizes */
  82. #define CX18_FW_CPU_SIZE (174716)
  83. #define CX18_FW_APU_SIZE (141200)
  84. #define APU_ROM_SYNC1 0x6D676553 /* "mgeS" */
  85. #define APU_ROM_SYNC2 0x72646548 /* "rdeH" */
  86. struct cx18_apu_rom_seghdr {
  87. u32 sync1;
  88. u32 sync2;
  89. u32 addr;
  90. u32 size;
  91. };
  92. static int load_cpu_fw_direct(const char *fn, u8 __iomem *mem, struct cx18 *cx, long size)
  93. {
  94. const struct firmware *fw = NULL;
  95. int retries = 3;
  96. int i, j;
  97. u32 __iomem *dst = (u32 __iomem *)mem;
  98. const u32 *src;
  99. retry:
  100. if (!retries || request_firmware(&fw, fn, &cx->dev->dev)) {
  101. CX18_ERR("Unable to open firmware %s (must be %ld bytes)\n",
  102. fn, size);
  103. CX18_ERR("Did you put the firmware in the hotplug firmware directory?\n");
  104. return -ENOMEM;
  105. }
  106. src = (const u32 *)fw->data;
  107. if (fw->size != size) {
  108. /* Due to race conditions in firmware loading (esp. with
  109. udev <0.95) the wrong file was sometimes loaded. So we check
  110. filesizes to see if at least the right-sized file was
  111. loaded. If not, then we retry. */
  112. CX18_INFO("retry: file loaded was not %s (expected size %ld, got %zd)\n",
  113. fn, size, fw->size);
  114. release_firmware(fw);
  115. retries--;
  116. goto retry;
  117. }
  118. for (i = 0; i < fw->size; i += 4096) {
  119. setup_page(i);
  120. for (j = i; j < fw->size && j < i + 4096; j += 4) {
  121. /* no need for endianness conversion on the ppc */
  122. __raw_writel(*src, dst);
  123. if (__raw_readl(dst) != *src) {
  124. CX18_ERR("Mismatch at offset %x\n", i);
  125. release_firmware(fw);
  126. return -EIO;
  127. }
  128. dst++;
  129. src++;
  130. }
  131. }
  132. if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))
  133. CX18_INFO("loaded %s firmware (%zd bytes)\n", fn, fw->size);
  134. release_firmware(fw);
  135. return size;
  136. }
  137. static int load_apu_fw_direct(const char *fn, u8 __iomem *dst, struct cx18 *cx, long size)
  138. {
  139. const struct firmware *fw = NULL;
  140. int retries = 3;
  141. int i, j;
  142. const u32 *src;
  143. struct cx18_apu_rom_seghdr seghdr;
  144. const u8 *vers;
  145. u32 offset = 0;
  146. u32 apu_version = 0;
  147. int sz;
  148. retry:
  149. if (!retries || request_firmware(&fw, fn, &cx->dev->dev)) {
  150. CX18_ERR("unable to open firmware %s (must be %ld bytes)\n",
  151. fn, size);
  152. CX18_ERR("did you put the firmware in the hotplug firmware directory?\n");
  153. return -ENOMEM;
  154. }
  155. src = (const u32 *)fw->data;
  156. vers = fw->data + sizeof(seghdr);
  157. sz = fw->size;
  158. if (fw->size != size) {
  159. /* Due to race conditions in firmware loading (esp. with
  160. udev <0.95) the wrong file was sometimes loaded. So we check
  161. filesizes to see if at least the right-sized file was
  162. loaded. If not, then we retry. */
  163. CX18_INFO("retry: file loaded was not %s (expected size %ld, got %zd)\n",
  164. fn, size, fw->size);
  165. release_firmware(fw);
  166. retries--;
  167. goto retry;
  168. }
  169. apu_version = (vers[0] << 24) | (vers[4] << 16) | vers[32];
  170. while (offset + sizeof(seghdr) < size) {
  171. /* TODO: byteswapping */
  172. memcpy(&seghdr, src + offset / 4, sizeof(seghdr));
  173. offset += sizeof(seghdr);
  174. if (seghdr.sync1 != APU_ROM_SYNC1 ||
  175. seghdr.sync2 != APU_ROM_SYNC2) {
  176. offset += seghdr.size;
  177. continue;
  178. }
  179. CX18_DEBUG_INFO("load segment %x-%x\n", seghdr.addr,
  180. seghdr.addr + seghdr.size - 1);
  181. if (offset + seghdr.size > sz)
  182. break;
  183. for (i = 0; i < seghdr.size; i += 4096) {
  184. setup_page(offset + i);
  185. for (j = i; j < seghdr.size && j < i + 4096; j += 4) {
  186. /* no need for endianness conversion on the ppc */
  187. __raw_writel(src[(offset + j) / 4], dst + seghdr.addr + j);
  188. if (__raw_readl(dst + seghdr.addr + j) != src[(offset + j) / 4]) {
  189. CX18_ERR("Mismatch at offset %x\n", offset + j);
  190. release_firmware(fw);
  191. return -EIO;
  192. }
  193. }
  194. }
  195. offset += seghdr.size;
  196. }
  197. if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))
  198. CX18_INFO("loaded %s firmware V%08x (%zd bytes)\n",
  199. fn, apu_version, fw->size);
  200. release_firmware(fw);
  201. /* Clear bit0 for APU to start from 0 */
  202. write_reg(read_reg(0xc72030) & ~1, 0xc72030);
  203. return size;
  204. }
  205. void cx18_halt_firmware(struct cx18 *cx)
  206. {
  207. CX18_DEBUG_INFO("Preparing for firmware halt.\n");
  208. write_reg(0x000F000F, CX18_PROC_SOFT_RESET); /* stop the fw */
  209. write_reg(0x00020002, CX18_ADEC_CONTROL);
  210. }
  211. void cx18_init_power(struct cx18 *cx, int lowpwr)
  212. {
  213. /* power-down Spare and AOM PLLs */
  214. /* power-up fast, slow and mpeg PLLs */
  215. write_reg(0x00000008, CX18_PLL_POWER_DOWN);
  216. /* ADEC out of sleep */
  217. write_reg(0x00020000, CX18_ADEC_CONTROL);
  218. /* The fast clock is at 200/245 MHz */
  219. write_reg(lowpwr ? 0xD : 0x11, CX18_FAST_CLOCK_PLL_INT);
  220. write_reg(lowpwr ? 0x1EFBF37 : 0x038E3D7, CX18_FAST_CLOCK_PLL_FRAC);
  221. write_reg(2, CX18_FAST_CLOCK_PLL_POST);
  222. write_reg(1, CX18_FAST_CLOCK_PLL_PRESCALE);
  223. write_reg(4, CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH);
  224. /* set slow clock to 125/120 MHz */
  225. write_reg(lowpwr ? 0x11 : 0x10, CX18_SLOW_CLOCK_PLL_INT);
  226. write_reg(lowpwr ? 0xEBAF05 : 0x18618A8, CX18_SLOW_CLOCK_PLL_FRAC);
  227. write_reg(4, CX18_SLOW_CLOCK_PLL_POST);
  228. /* mpeg clock pll 54MHz */
  229. write_reg(0xF, CX18_MPEG_CLOCK_PLL_INT);
  230. write_reg(0x2BCFEF, CX18_MPEG_CLOCK_PLL_FRAC);
  231. write_reg(8, CX18_MPEG_CLOCK_PLL_POST);
  232. /* Defaults */
  233. /* APU = SC or SC/2 = 125/62.5 */
  234. /* EPU = SC = 125 */
  235. /* DDR = FC = 180 */
  236. /* ENC = SC = 125 */
  237. /* AI1 = SC = 125 */
  238. /* VIM2 = disabled */
  239. /* PCI = FC/2 = 90 */
  240. /* AI2 = disabled */
  241. /* DEMUX = disabled */
  242. /* AO = SC/2 = 62.5 */
  243. /* SER = 54MHz */
  244. /* VFC = disabled */
  245. /* USB = disabled */
  246. write_reg(lowpwr ? 0xFFFF0020 : 0x00060004, CX18_CLOCK_SELECT1);
  247. write_reg(lowpwr ? 0xFFFF0004 : 0x00060006, CX18_CLOCK_SELECT2);
  248. write_reg(0xFFFF0002, CX18_HALF_CLOCK_SELECT1);
  249. write_reg(0xFFFF0104, CX18_HALF_CLOCK_SELECT2);
  250. write_reg(0xFFFF9026, CX18_CLOCK_ENABLE1);
  251. write_reg(0xFFFF3105, CX18_CLOCK_ENABLE2);
  252. }
  253. void cx18_init_memory(struct cx18 *cx)
  254. {
  255. cx18_msleep_timeout(10, 0);
  256. write_reg(0x10000, CX18_DDR_SOFT_RESET);
  257. cx18_msleep_timeout(10, 0);
  258. write_reg(cx->card->ddr.chip_config, CX18_DDR_CHIP_CONFIG);
  259. cx18_msleep_timeout(10, 0);
  260. write_reg(cx->card->ddr.refresh, CX18_DDR_REFRESH);
  261. write_reg(cx->card->ddr.timing1, CX18_DDR_TIMING1);
  262. write_reg(cx->card->ddr.timing2, CX18_DDR_TIMING2);
  263. cx18_msleep_timeout(10, 0);
  264. /* Initialize DQS pad time */
  265. write_reg(cx->card->ddr.tune_lane, CX18_DDR_TUNE_LANE);
  266. write_reg(cx->card->ddr.initial_emrs, CX18_DDR_INITIAL_EMRS);
  267. cx18_msleep_timeout(10, 0);
  268. write_reg(0x20000, CX18_DDR_SOFT_RESET);
  269. cx18_msleep_timeout(10, 0);
  270. /* use power-down mode when idle */
  271. write_reg(0x00000010, CX18_DDR_POWER_REG);
  272. write_reg(0x10001, CX18_REG_BUS_TIMEOUT_EN);
  273. write_reg(0x48, CX18_DDR_MB_PER_ROW_7);
  274. write_reg(0xE0000, CX18_DDR_BASE_63_ADDR);
  275. write_reg(0x00000101, CX18_WMB_CLIENT02); /* AO */
  276. write_reg(0x00000101, CX18_WMB_CLIENT09); /* AI2 */
  277. write_reg(0x00000101, CX18_WMB_CLIENT05); /* VIM1 */
  278. write_reg(0x00000101, CX18_WMB_CLIENT06); /* AI1 */
  279. write_reg(0x00000101, CX18_WMB_CLIENT07); /* 3D comb */
  280. write_reg(0x00000101, CX18_WMB_CLIENT10); /* ME */
  281. write_reg(0x00000101, CX18_WMB_CLIENT12); /* ENC */
  282. write_reg(0x00000101, CX18_WMB_CLIENT13); /* PK */
  283. write_reg(0x00000101, CX18_WMB_CLIENT11); /* RC */
  284. write_reg(0x00000101, CX18_WMB_CLIENT14); /* AVO */
  285. }
  286. int cx18_firmware_init(struct cx18 *cx)
  287. {
  288. /* Allow chip to control CLKRUN */
  289. write_reg(0x5, CX18_DSP0_INTERRUPT_MASK);
  290. write_reg(0x000F000F, CX18_PROC_SOFT_RESET); /* stop the fw */
  291. cx18_msleep_timeout(1, 0);
  292. sw1_irq_enable(IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
  293. sw2_irq_enable(IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
  294. /* Only if the processor is not running */
  295. if (read_reg(CX18_PROC_SOFT_RESET) & 8) {
  296. int sz = load_apu_fw_direct("v4l-cx23418-apu.fw",
  297. cx->enc_mem, cx, CX18_FW_APU_SIZE);
  298. sz = sz <= 0 ? sz : load_cpu_fw_direct("v4l-cx23418-cpu.fw",
  299. cx->enc_mem, cx, CX18_FW_CPU_SIZE);
  300. if (sz > 0) {
  301. int retries = 0;
  302. /* start the CPU */
  303. write_reg(0x00080000, CX18_PROC_SOFT_RESET);
  304. while (retries++ < 50) { /* Loop for max 500mS */
  305. if ((read_reg(CX18_PROC_SOFT_RESET) & 1) == 0)
  306. break;
  307. cx18_msleep_timeout(10, 0);
  308. }
  309. cx18_msleep_timeout(200, 0);
  310. if (retries == 51) {
  311. CX18_ERR("Could not start the CPU\n");
  312. return -EIO;
  313. }
  314. }
  315. if (sz <= 0)
  316. return -EIO;
  317. }
  318. /* initialize GPIO */
  319. write_reg(0x14001400, 0xC78110);
  320. return 0;
  321. }