Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000-2005, DENX Software Engineering |
| 3 | * Wolfgang Denk <wd@denx.de> |
| 4 | * Copyright (C) Procsys. All rights reserved. |
| 5 | * Mushtaq Khan <mushtaq_k@procsys.com> |
| 6 | * <mushtaqk_921@yahoo.co.in> |
| 7 | * Copyright (C) 2008 Freescale Semiconductor, Inc. |
| 8 | * Dave Liu <daveliu@freescale.com> |
| 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0+ |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 14 | #include <dm.h> |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 15 | #include <sata.h> |
| 16 | |
| 17 | struct blk_desc sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE]; |
| 18 | |
| 19 | #ifdef CONFIG_PARTITIONS |
| 20 | struct blk_desc *sata_get_dev(int dev) |
| 21 | { |
| 22 | return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL; |
| 23 | } |
| 24 | #endif |
| 25 | |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 26 | #ifdef CONFIG_BLK |
| 27 | static unsigned long sata_bread(struct udevice *dev, lbaint_t start, |
| 28 | lbaint_t blkcnt, void *dst) |
| 29 | { |
| 30 | return -ENOSYS; |
| 31 | } |
| 32 | |
| 33 | static unsigned long sata_bwrite(struct udevice *dev, lbaint_t start, |
| 34 | lbaint_t blkcnt, const void *buffer) |
| 35 | { |
| 36 | return -ENOSYS; |
| 37 | } |
| 38 | #else |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 39 | static unsigned long sata_bread(struct blk_desc *block_dev, lbaint_t start, |
| 40 | lbaint_t blkcnt, void *dst) |
| 41 | { |
| 42 | return sata_read(block_dev->devnum, start, blkcnt, dst); |
| 43 | } |
| 44 | |
| 45 | static unsigned long sata_bwrite(struct blk_desc *block_dev, lbaint_t start, |
| 46 | lbaint_t blkcnt, const void *buffer) |
| 47 | { |
| 48 | return sata_write(block_dev->devnum, start, blkcnt, buffer); |
| 49 | } |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 50 | #endif |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 51 | |
| 52 | int __sata_initialize(void) |
| 53 | { |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 54 | int rc, ret = -1; |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 55 | int i; |
| 56 | |
| 57 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) { |
| 58 | memset(&sata_dev_desc[i], 0, sizeof(struct blk_desc)); |
| 59 | sata_dev_desc[i].if_type = IF_TYPE_SATA; |
| 60 | sata_dev_desc[i].devnum = i; |
| 61 | sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN; |
| 62 | sata_dev_desc[i].type = DEV_TYPE_HARDDISK; |
| 63 | sata_dev_desc[i].lba = 0; |
| 64 | sata_dev_desc[i].blksz = 512; |
| 65 | sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz); |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 66 | #ifndef CONFIG_BLK |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 67 | sata_dev_desc[i].block_read = sata_bread; |
| 68 | sata_dev_desc[i].block_write = sata_bwrite; |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 69 | #endif |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 70 | rc = init_sata(i); |
| 71 | if (!rc) { |
| 72 | rc = scan_sata(i); |
| 73 | if (!rc && sata_dev_desc[i].lba > 0 && |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 74 | sata_dev_desc[i].blksz > 0) { |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 75 | part_init(&sata_dev_desc[i]); |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 76 | ret = i; |
| 77 | } |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 81 | return ret; |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 82 | } |
| 83 | int sata_initialize(void) __attribute__((weak, alias("__sata_initialize"))); |
| 84 | |
| 85 | __weak int __sata_stop(void) |
| 86 | { |
| 87 | int i, err = 0; |
| 88 | |
| 89 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) |
| 90 | err |= reset_sata(i); |
| 91 | |
| 92 | if (err) |
| 93 | printf("Could not reset some SATA devices\n"); |
| 94 | |
| 95 | return err; |
| 96 | } |
| 97 | int sata_stop(void) __attribute__((weak, alias("__sata_stop"))); |
| 98 | |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 99 | #ifdef CONFIG_BLK |
| 100 | static const struct blk_ops sata_blk_ops = { |
| 101 | .read = sata_bread, |
| 102 | .write = sata_bwrite, |
| 103 | }; |
| 104 | |
| 105 | U_BOOT_DRIVER(sata_blk) = { |
| 106 | .name = "sata_blk", |
| 107 | .id = UCLASS_BLK, |
| 108 | .ops = &sata_blk_ops, |
| 109 | }; |
| 110 | #else |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 111 | U_BOOT_LEGACY_BLK(sata) = { |
| 112 | .if_typename = "sata", |
| 113 | .if_type = IF_TYPE_SATA, |
| 114 | .max_devs = CONFIG_SYS_SATA_MAX_DEVICE, |
| 115 | .desc = sata_dev_desc, |
| 116 | }; |
Simon Glass | f5a14af | 2016-05-01 11:36:26 -0600 | [diff] [blame] | 117 | #endif |