Chin Liang See | cca9f45 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2013 Altera Corporation <www.altera.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <malloc.h> |
| 9 | #include <dwmmc.h> |
Pavel Machek | 51d2113 | 2014-09-08 14:08:45 +0200 | [diff] [blame] | 10 | #include <errno.h> |
Chin Liang See | cca9f45 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 11 | #include <asm/arch/dwmmc.h> |
| 12 | #include <asm/arch/clock_manager.h> |
| 13 | #include <asm/arch/system_manager.h> |
| 14 | |
| 15 | static const struct socfpga_clock_manager *clock_manager_base = |
| 16 | (void *)SOCFPGA_CLKMGR_ADDRESS; |
| 17 | static const struct socfpga_system_manager *system_manager_base = |
| 18 | (void *)SOCFPGA_SYSMGR_ADDRESS; |
| 19 | |
Chin Liang See | cca9f45 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 20 | static void socfpga_dwmci_clksel(struct dwmci_host *host) |
| 21 | { |
| 22 | unsigned int drvsel; |
| 23 | unsigned int smplsel; |
| 24 | |
| 25 | /* Disable SDMMC clock. */ |
Pavel Machek | 91c2f8f | 2014-07-19 23:57:59 +0200 | [diff] [blame] | 26 | clrbits_le32(&clock_manager_base->per_pll.en, |
Chin Liang See | cca9f45 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 27 | CLKMGR_PERPLLGRP_EN_SDMMCCLK_MASK); |
| 28 | |
| 29 | /* Configures drv_sel and smpl_sel */ |
| 30 | drvsel = CONFIG_SOCFPGA_DWMMC_DRVSEL; |
| 31 | smplsel = CONFIG_SOCFPGA_DWMMC_SMPSEL; |
| 32 | |
| 33 | debug("%s: drvsel %d smplsel %d\n", __func__, drvsel, smplsel); |
| 34 | writel(SYSMGR_SDMMC_CTRL_SET(smplsel, drvsel), |
| 35 | &system_manager_base->sdmmcgrp_ctrl); |
| 36 | |
| 37 | debug("%s: SYSMGR_SDMMCGRP_CTRL_REG = 0x%x\n", __func__, |
| 38 | readl(&system_manager_base->sdmmcgrp_ctrl)); |
| 39 | |
| 40 | /* Enable SDMMC clock */ |
Pavel Machek | 91c2f8f | 2014-07-19 23:57:59 +0200 | [diff] [blame] | 41 | setbits_le32(&clock_manager_base->per_pll.en, |
Chin Liang See | cca9f45 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 42 | CLKMGR_PERPLLGRP_EN_SDMMCCLK_MASK); |
| 43 | } |
| 44 | |
| 45 | int socfpga_dwmmc_init(u32 regbase, int bus_width, int index) |
| 46 | { |
Pavel Machek | 1d45a44 | 2014-07-21 13:30:19 +0200 | [diff] [blame] | 47 | struct dwmci_host *host; |
Pavel Machek | 51d2113 | 2014-09-08 14:08:45 +0200 | [diff] [blame] | 48 | unsigned long clk = cm_get_mmc_controller_clk_hz(); |
| 49 | |
| 50 | if (clk == 0) { |
| 51 | printf("%s: MMC clock is zero!", __func__); |
| 52 | return -EINVAL; |
| 53 | } |
Pavel Machek | 1d45a44 | 2014-07-21 13:30:19 +0200 | [diff] [blame] | 54 | |
| 55 | /* calloc for zero init */ |
Pavel Machek | 51d2113 | 2014-09-08 14:08:45 +0200 | [diff] [blame] | 56 | host = calloc(1, sizeof(struct dwmci_host)); |
Chin Liang See | cca9f45 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 57 | if (!host) { |
Pavel Machek | 51d2113 | 2014-09-08 14:08:45 +0200 | [diff] [blame] | 58 | printf("%s: calloc() failed!\n", __func__); |
| 59 | return -ENOMEM; |
Chin Liang See | cca9f45 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 60 | } |
| 61 | |
Pavel Machek | 1d45a44 | 2014-07-21 13:30:19 +0200 | [diff] [blame] | 62 | host->name = "SOCFPGA DWMMC"; |
Chin Liang See | cca9f45 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 63 | host->ioaddr = (void *)regbase; |
| 64 | host->buswidth = bus_width; |
| 65 | host->clksel = socfpga_dwmci_clksel; |
| 66 | host->dev_index = index; |
| 67 | /* fixed clock divide by 4 which due to the SDMMC wrapper */ |
Pavel Machek | 51d2113 | 2014-09-08 14:08:45 +0200 | [diff] [blame] | 68 | host->bus_hz = clk; |
Chin Liang See | cca9f45 | 2013-12-30 18:26:14 -0600 | [diff] [blame] | 69 | host->fifoth_val = MSIZE(0x2) | |
| 70 | RX_WMARK(CONFIG_SOCFPGA_DWMMC_FIFO_DEPTH / 2 - 1) | |
| 71 | TX_WMARK(CONFIG_SOCFPGA_DWMMC_FIFO_DEPTH / 2); |
| 72 | |
| 73 | return add_dwmci(host, host->bus_hz, 400000); |
| 74 | } |
| 75 | |