0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/module.h>
0012 #include <linux/device.h>
0013 #include <linux/mutex.h>
0014 #include <linux/delay.h>
0015 #include <linux/of_platform.h>
0016 #include <linux/io.h>
0017
0018 #include <asm/octeon/octeon.h>
0019
0020
0021 union cvm_usbdrd_uctl_ctl {
0022 uint64_t u64;
0023 struct cvm_usbdrd_uctl_ctl_s {
0024
0025 __BITFIELD_FIELD(uint64_t clear_bist:1,
0026
0027 __BITFIELD_FIELD(uint64_t start_bist:1,
0028
0029
0030
0031
0032
0033
0034
0035
0036 __BITFIELD_FIELD(uint64_t ref_clk_sel:2,
0037
0038 __BITFIELD_FIELD(uint64_t ssc_en:1,
0039
0040
0041
0042
0043
0044
0045 __BITFIELD_FIELD(uint64_t ssc_range:3,
0046
0047
0048
0049
0050 __BITFIELD_FIELD(uint64_t ssc_ref_clk_sel:9,
0051
0052
0053
0054
0055
0056
0057 __BITFIELD_FIELD(uint64_t mpll_multiplier:7,
0058
0059
0060
0061 __BITFIELD_FIELD(uint64_t ref_ssp_en:1,
0062
0063
0064
0065
0066
0067
0068
0069 __BITFIELD_FIELD(uint64_t ref_clk_div2:1,
0070
0071
0072
0073
0074 __BITFIELD_FIELD(uint64_t ref_clk_fsel:6,
0075
0076 __BITFIELD_FIELD(uint64_t reserved_31_31:1,
0077
0078 __BITFIELD_FIELD(uint64_t h_clk_en:1,
0079
0080
0081
0082
0083 __BITFIELD_FIELD(uint64_t h_clk_byp_sel:1,
0084
0085 __BITFIELD_FIELD(uint64_t h_clkdiv_rst:1,
0086
0087 __BITFIELD_FIELD(uint64_t reserved_27_27:1,
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 __BITFIELD_FIELD(uint64_t h_clkdiv_sel:3,
0099
0100 __BITFIELD_FIELD(uint64_t reserved_22_23:2,
0101
0102 __BITFIELD_FIELD(uint64_t usb3_port_perm_attach:1,
0103
0104 __BITFIELD_FIELD(uint64_t usb2_port_perm_attach:1,
0105
0106 __BITFIELD_FIELD(uint64_t reserved_19_19:1,
0107
0108 __BITFIELD_FIELD(uint64_t usb3_port_disable:1,
0109
0110 __BITFIELD_FIELD(uint64_t reserved_17_17:1,
0111
0112 __BITFIELD_FIELD(uint64_t usb2_port_disable:1,
0113
0114 __BITFIELD_FIELD(uint64_t reserved_15_15:1,
0115
0116 __BITFIELD_FIELD(uint64_t ss_power_en:1,
0117
0118 __BITFIELD_FIELD(uint64_t reserved_13_13:1,
0119
0120 __BITFIELD_FIELD(uint64_t hs_power_en:1,
0121
0122 __BITFIELD_FIELD(uint64_t reserved_5_11:7,
0123
0124 __BITFIELD_FIELD(uint64_t csclk_en:1,
0125
0126 __BITFIELD_FIELD(uint64_t drd_mode:1,
0127
0128 __BITFIELD_FIELD(uint64_t uphy_rst:1,
0129
0130 __BITFIELD_FIELD(uint64_t uahc_rst:1,
0131
0132 __BITFIELD_FIELD(uint64_t uctl_rst:1,
0133 ;)))))))))))))))))))))))))))))))))
0134 } s;
0135 };
0136
0137
0138 union cvm_usbdrd_uctl_host_cfg {
0139 uint64_t u64;
0140 struct cvm_usbdrd_uctl_host_cfg_s {
0141
0142 __BITFIELD_FIELD(uint64_t reserved_60_63:4,
0143
0144 __BITFIELD_FIELD(uint64_t host_current_belt:12,
0145
0146 __BITFIELD_FIELD(uint64_t reserved_38_47:10,
0147
0148 __BITFIELD_FIELD(uint64_t fla:6,
0149
0150 __BITFIELD_FIELD(uint64_t reserved_29_31:3,
0151
0152 __BITFIELD_FIELD(uint64_t bme:1,
0153
0154 __BITFIELD_FIELD(uint64_t oci_en:1,
0155
0156
0157
0158
0159 __BITFIELD_FIELD(uint64_t oci_active_high_en:1,
0160
0161 __BITFIELD_FIELD(uint64_t ppc_en:1,
0162
0163
0164
0165
0166 __BITFIELD_FIELD(uint64_t ppc_active_high_en:1,
0167
0168 __BITFIELD_FIELD(uint64_t reserved_0_23:24,
0169 ;)))))))))))
0170 } s;
0171 };
0172
0173
0174 union cvm_usbdrd_uctl_shim_cfg {
0175 uint64_t u64;
0176 struct cvm_usbdrd_uctl_shim_cfg_s {
0177
0178 __BITFIELD_FIELD(uint64_t xs_ncb_oob_wrn:1,
0179
0180 __BITFIELD_FIELD(uint64_t reserved_60_62:3,
0181
0182
0183
0184
0185
0186
0187 __BITFIELD_FIELD(uint64_t xs_ncb_oob_osrc:12,
0188
0189 __BITFIELD_FIELD(uint64_t xm_bad_dma_wrn:1,
0190
0191 __BITFIELD_FIELD(uint64_t reserved_44_46:3,
0192
0193 __BITFIELD_FIELD(uint64_t xm_bad_dma_type:4,
0194
0195 __BITFIELD_FIELD(uint64_t reserved_13_39:27,
0196
0197 __BITFIELD_FIELD(uint64_t dma_read_cmd:1,
0198
0199 __BITFIELD_FIELD(uint64_t reserved_10_11:2,
0200
0201
0202
0203
0204
0205
0206 __BITFIELD_FIELD(uint64_t dma_endian_mode:2,
0207
0208 __BITFIELD_FIELD(uint64_t reserved_2_7:6,
0209
0210
0211
0212
0213
0214
0215 __BITFIELD_FIELD(uint64_t csr_endian_mode:2,
0216 ;))))))))))))
0217 } s;
0218 };
0219
0220 #define OCTEON_H_CLKDIV_SEL 8
0221 #define OCTEON_MIN_H_CLK_RATE 150000000
0222 #define OCTEON_MAX_H_CLK_RATE 300000000
0223
0224 static DEFINE_MUTEX(dwc3_octeon_clocks_mutex);
0225 static uint8_t clk_div[OCTEON_H_CLKDIV_SEL] = {1, 2, 4, 6, 8, 16, 24, 32};
0226
0227
0228 static int dwc3_octeon_config_power(struct device *dev, u64 base)
0229 {
0230 #define UCTL_HOST_CFG 0xe0
0231 union cvm_usbdrd_uctl_host_cfg uctl_host_cfg;
0232 union cvmx_gpio_bit_cfgx gpio_bit;
0233 uint32_t gpio_pwr[3];
0234 int gpio, len, power_active_low;
0235 struct device_node *node = dev->of_node;
0236 int index = (base >> 24) & 1;
0237
0238 if (of_find_property(node, "power", &len) != NULL) {
0239 if (len == 12) {
0240 of_property_read_u32_array(node, "power", gpio_pwr, 3);
0241 power_active_low = gpio_pwr[2] & 0x01;
0242 gpio = gpio_pwr[1];
0243 } else if (len == 8) {
0244 of_property_read_u32_array(node, "power", gpio_pwr, 2);
0245 power_active_low = 0;
0246 gpio = gpio_pwr[1];
0247 } else {
0248 dev_err(dev, "dwc3 controller clock init failure.\n");
0249 return -EINVAL;
0250 }
0251 if ((OCTEON_IS_MODEL(OCTEON_CN73XX) ||
0252 OCTEON_IS_MODEL(OCTEON_CNF75XX))
0253 && gpio <= 31) {
0254 gpio_bit.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(gpio));
0255 gpio_bit.s.tx_oe = 1;
0256 gpio_bit.s.output_sel = (index == 0 ? 0x14 : 0x15);
0257 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(gpio), gpio_bit.u64);
0258 } else if (gpio <= 15) {
0259 gpio_bit.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(gpio));
0260 gpio_bit.s.tx_oe = 1;
0261 gpio_bit.s.output_sel = (index == 0 ? 0x14 : 0x19);
0262 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(gpio), gpio_bit.u64);
0263 } else {
0264 gpio_bit.u64 = cvmx_read_csr(CVMX_GPIO_XBIT_CFGX(gpio));
0265 gpio_bit.s.tx_oe = 1;
0266 gpio_bit.s.output_sel = (index == 0 ? 0x14 : 0x19);
0267 cvmx_write_csr(CVMX_GPIO_XBIT_CFGX(gpio), gpio_bit.u64);
0268 }
0269
0270
0271 uctl_host_cfg.u64 = cvmx_read_csr(base + UCTL_HOST_CFG);
0272 uctl_host_cfg.s.ppc_en = 1;
0273 uctl_host_cfg.s.ppc_active_high_en = !power_active_low;
0274 cvmx_write_csr(base + UCTL_HOST_CFG, uctl_host_cfg.u64);
0275 } else {
0276
0277 uctl_host_cfg.u64 = cvmx_read_csr(base + UCTL_HOST_CFG);
0278 uctl_host_cfg.s.ppc_en = 0;
0279 uctl_host_cfg.s.ppc_active_high_en = 0;
0280 cvmx_write_csr(base + UCTL_HOST_CFG, uctl_host_cfg.u64);
0281 dev_warn(dev, "dwc3 controller clock init failure.\n");
0282 }
0283 return 0;
0284 }
0285
0286 static int dwc3_octeon_clocks_start(struct device *dev, u64 base)
0287 {
0288 union cvm_usbdrd_uctl_ctl uctl_ctl;
0289 int ref_clk_sel = 2;
0290 u64 div;
0291 u32 clock_rate;
0292 int mpll_mul;
0293 int i;
0294 u64 h_clk_rate;
0295 u64 uctl_ctl_reg = base;
0296
0297 if (dev->of_node) {
0298 const char *ss_clock_type;
0299 const char *hs_clock_type;
0300
0301 i = of_property_read_u32(dev->of_node,
0302 "refclk-frequency", &clock_rate);
0303 if (i) {
0304 pr_err("No UCTL \"refclk-frequency\"\n");
0305 return -EINVAL;
0306 }
0307 i = of_property_read_string(dev->of_node,
0308 "refclk-type-ss", &ss_clock_type);
0309 if (i) {
0310 pr_err("No UCTL \"refclk-type-ss\"\n");
0311 return -EINVAL;
0312 }
0313 i = of_property_read_string(dev->of_node,
0314 "refclk-type-hs", &hs_clock_type);
0315 if (i) {
0316 pr_err("No UCTL \"refclk-type-hs\"\n");
0317 return -EINVAL;
0318 }
0319 if (strcmp("dlmc_ref_clk0", ss_clock_type) == 0) {
0320 if (strcmp(hs_clock_type, "dlmc_ref_clk0") == 0)
0321 ref_clk_sel = 0;
0322 else if (strcmp(hs_clock_type, "pll_ref_clk") == 0)
0323 ref_clk_sel = 2;
0324 else
0325 pr_err("Invalid HS clock type %s, using pll_ref_clk instead\n",
0326 hs_clock_type);
0327 } else if (strcmp(ss_clock_type, "dlmc_ref_clk1") == 0) {
0328 if (strcmp(hs_clock_type, "dlmc_ref_clk1") == 0)
0329 ref_clk_sel = 1;
0330 else if (strcmp(hs_clock_type, "pll_ref_clk") == 0)
0331 ref_clk_sel = 3;
0332 else {
0333 pr_err("Invalid HS clock type %s, using pll_ref_clk instead\n",
0334 hs_clock_type);
0335 ref_clk_sel = 3;
0336 }
0337 } else
0338 pr_err("Invalid SS clock type %s, using dlmc_ref_clk0 instead\n",
0339 ss_clock_type);
0340
0341 if ((ref_clk_sel == 0 || ref_clk_sel == 1) &&
0342 (clock_rate != 100000000))
0343 pr_err("Invalid UCTL clock rate of %u, using 100000000 instead\n",
0344 clock_rate);
0345
0346 } else {
0347 pr_err("No USB UCTL device node\n");
0348 return -EINVAL;
0349 }
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359 uctl_ctl.u64 = cvmx_read_csr(uctl_ctl_reg);
0360 uctl_ctl.s.uphy_rst = 1;
0361 uctl_ctl.s.uahc_rst = 1;
0362 uctl_ctl.s.uctl_rst = 1;
0363 cvmx_write_csr(uctl_ctl_reg, uctl_ctl.u64);
0364
0365
0366 uctl_ctl.u64 = cvmx_read_csr(uctl_ctl_reg);
0367 uctl_ctl.s.h_clkdiv_rst = 1;
0368 cvmx_write_csr(uctl_ctl_reg, uctl_ctl.u64);
0369
0370
0371 for (div = 0; div < OCTEON_H_CLKDIV_SEL; div++) {
0372 h_clk_rate = octeon_get_io_clock_rate() / clk_div[div];
0373 if (h_clk_rate <= OCTEON_MAX_H_CLK_RATE &&
0374 h_clk_rate >= OCTEON_MIN_H_CLK_RATE)
0375 break;
0376 }
0377 uctl_ctl.u64 = cvmx_read_csr(uctl_ctl_reg);
0378 uctl_ctl.s.h_clkdiv_sel = div;
0379 uctl_ctl.s.h_clk_en = 1;
0380 cvmx_write_csr(uctl_ctl_reg, uctl_ctl.u64);
0381 uctl_ctl.u64 = cvmx_read_csr(uctl_ctl_reg);
0382 if ((div != uctl_ctl.s.h_clkdiv_sel) || (!uctl_ctl.s.h_clk_en)) {
0383 dev_err(dev, "dwc3 controller clock init failure.\n");
0384 return -EINVAL;
0385 }
0386
0387
0388 uctl_ctl.u64 = cvmx_read_csr(uctl_ctl_reg);
0389 uctl_ctl.s.h_clkdiv_rst = 0;
0390 cvmx_write_csr(uctl_ctl_reg, uctl_ctl.u64);
0391
0392
0393 uctl_ctl.u64 = cvmx_read_csr(uctl_ctl_reg);
0394 uctl_ctl.s.ref_clk_sel = ref_clk_sel;
0395 uctl_ctl.s.ref_clk_fsel = 0x07;
0396 uctl_ctl.s.ref_clk_div2 = 0;
0397 switch (clock_rate) {
0398 default:
0399 dev_err(dev, "Invalid ref_clk %u, using 100000000 instead\n",
0400 clock_rate);
0401 fallthrough;
0402 case 100000000:
0403 mpll_mul = 0x19;
0404 if (ref_clk_sel < 2)
0405 uctl_ctl.s.ref_clk_fsel = 0x27;
0406 break;
0407 case 50000000:
0408 mpll_mul = 0x32;
0409 break;
0410 case 125000000:
0411 mpll_mul = 0x28;
0412 break;
0413 }
0414 uctl_ctl.s.mpll_multiplier = mpll_mul;
0415
0416
0417 uctl_ctl.s.ssc_en = 1;
0418
0419
0420 uctl_ctl.s.ref_ssp_en = 1;
0421
0422
0423
0424
0425 uctl_ctl.s.hs_power_en = 1;
0426 uctl_ctl.s.ss_power_en = 1;
0427 cvmx_write_csr(uctl_ctl_reg, uctl_ctl.u64);
0428
0429
0430 udelay(10);
0431
0432
0433 uctl_ctl.u64 = cvmx_read_csr(uctl_ctl_reg);
0434 uctl_ctl.s.uctl_rst = 0;
0435 cvmx_write_csr(uctl_ctl_reg, uctl_ctl.u64);
0436
0437
0438 udelay(10);
0439
0440
0441 if (dwc3_octeon_config_power(dev, base)) {
0442 dev_err(dev, "Error configuring power.\n");
0443 return -EINVAL;
0444 }
0445
0446
0447 uctl_ctl.u64 = cvmx_read_csr(uctl_ctl_reg);
0448 uctl_ctl.s.uahc_rst = 0;
0449 cvmx_write_csr(uctl_ctl_reg, uctl_ctl.u64);
0450
0451
0452 udelay(10);
0453
0454
0455 uctl_ctl.u64 = cvmx_read_csr(uctl_ctl_reg);
0456 uctl_ctl.s.csclk_en = 1;
0457 cvmx_write_csr(uctl_ctl_reg, uctl_ctl.u64);
0458
0459
0460 uctl_ctl.u64 = cvmx_read_csr(uctl_ctl_reg);
0461 uctl_ctl.s.drd_mode = 0;
0462 cvmx_write_csr(uctl_ctl_reg, uctl_ctl.u64);
0463
0464 return 0;
0465 }
0466
0467 static void __init dwc3_octeon_set_endian_mode(u64 base)
0468 {
0469 #define UCTL_SHIM_CFG 0xe8
0470 union cvm_usbdrd_uctl_shim_cfg shim_cfg;
0471
0472 shim_cfg.u64 = cvmx_read_csr(base + UCTL_SHIM_CFG);
0473 #ifdef __BIG_ENDIAN
0474 shim_cfg.s.dma_endian_mode = 1;
0475 shim_cfg.s.csr_endian_mode = 1;
0476 #else
0477 shim_cfg.s.dma_endian_mode = 0;
0478 shim_cfg.s.csr_endian_mode = 0;
0479 #endif
0480 cvmx_write_csr(base + UCTL_SHIM_CFG, shim_cfg.u64);
0481 }
0482
0483 #define CVMX_USBDRDX_UCTL_CTL(index) \
0484 (CVMX_ADD_IO_SEG(0x0001180068000000ull) + \
0485 ((index & 1) * 0x1000000ull))
0486 static void __init dwc3_octeon_phy_reset(u64 base)
0487 {
0488 union cvm_usbdrd_uctl_ctl uctl_ctl;
0489 int index = (base >> 24) & 1;
0490
0491 uctl_ctl.u64 = cvmx_read_csr(CVMX_USBDRDX_UCTL_CTL(index));
0492 uctl_ctl.s.uphy_rst = 0;
0493 cvmx_write_csr(CVMX_USBDRDX_UCTL_CTL(index), uctl_ctl.u64);
0494 }
0495
0496 static int __init dwc3_octeon_device_init(void)
0497 {
0498 const char compat_node_name[] = "cavium,octeon-7130-usb-uctl";
0499 struct platform_device *pdev;
0500 struct device_node *node;
0501 struct resource *res;
0502 void __iomem *base;
0503
0504
0505
0506
0507
0508 node = NULL;
0509 do {
0510 node = of_find_node_by_name(node, "uctl");
0511 if (!node)
0512 return -ENODEV;
0513
0514 if (of_device_is_compatible(node, compat_node_name)) {
0515 pdev = of_find_device_by_node(node);
0516 if (!pdev)
0517 return -ENODEV;
0518
0519
0520
0521
0522
0523
0524
0525 base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
0526 if (IS_ERR(base)) {
0527 put_device(&pdev->dev);
0528 return PTR_ERR(base);
0529 }
0530
0531 mutex_lock(&dwc3_octeon_clocks_mutex);
0532 dwc3_octeon_clocks_start(&pdev->dev, (u64)base);
0533 dwc3_octeon_set_endian_mode((u64)base);
0534 dwc3_octeon_phy_reset((u64)base);
0535 dev_info(&pdev->dev, "clocks initialized.\n");
0536 mutex_unlock(&dwc3_octeon_clocks_mutex);
0537 devm_iounmap(&pdev->dev, base);
0538 devm_release_mem_region(&pdev->dev, res->start,
0539 resource_size(res));
0540 put_device(&pdev->dev);
0541 }
0542 } while (node != NULL);
0543
0544 return 0;
0545 }
0546 device_initcall(dwc3_octeon_device_init);
0547
0548 MODULE_AUTHOR("David Daney <david.daney@cavium.com>");
0549 MODULE_LICENSE("GPL");
0550 MODULE_DESCRIPTION("USB driver for OCTEON III SoC");